Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Delivering the MVP Validation

Can't pass

Keeps saying "mFirstName" should pass but failed.

TeacherAssistant.java
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    // These things should be verified:
    // 1.  Member fields must start with an 'm'
    // 2.  The second letter in the field name must be uppercased to ensure camel-casing
    // NOTE:  To check if something is not equal use the != symbol. eg: 3 != 4
    if (fieldName.indexOf('m') != 0) {
      throw new IllegalArgumentException("m must be at index 0");
    }
    if (Character.isLetter(fieldName.indexOf(1)) && Character.isLowerCase(fieldName.indexOf(1)) || !Character.isLetter(fieldName.indexOf(1))) {
      throw new IllegalArgumentException("must be camel case");
    }
    return fieldName;
  }

}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Pixel HD Hi there! You're doing great, but it seems like you've mixed up indexOf and charAt a bit. If I modify your code just a teensy bit and rework the check for the lower case m then change out the indexOf to charAt, it passes! Take a look at your modified code:

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    // These things should be verified:
    // 1.  Member fields must start with an 'm'
    // 2.  The second letter in the field name must be uppercased to ensure camel-casing
    // NOTE:  To check if something is not equal use the != symbol. eg: 3 != 4
    if (fieldName.charAt(0) != 'm') {
      throw new IllegalArgumentException("m must be at index 0");
    }
    if (Character.isLetter(fieldName.charAt(1)) && Character.isLowerCase(fieldName.charAt(1)) || !Character.isLetter(fieldName.charAt(1))) {
      throw new IllegalArgumentException("must be camel case");
    }
    return fieldName;
  }
}

indexOf searches a string and then reports back the index where it found that character or substring. charAt looks at a specific index. Hope this is helpful! :smiley:

Thomas Nilsen
Thomas Nilsen
14,957 Points

I was just about to answer as well :)

I do think the last if could be written more easily:

if (!Character.isUpperCase(fieldName.charAt(1))) {
            throw new IllegalArgumentException("must be camel case");
        }

**Edit

I read your code way to quickly - you just had an extra check, which is good! :)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I agree with you, though Thomas Nilsen. For my own personal solution I combined the check. And there is at least one time that Treehouse send in a string like m_something_something. Which means that the challenge will fail if it doesn't check to make sure the character at the index of 1 is a letter. Here was my check/throw statement:

    if (fieldName.charAt(0) != 'm' || !Character.isLetter(fieldName.charAt(1)) || Character.isLowerCase(fieldName.charAt(1)))    {
      throw new IllegalArgumentException("Invalid format");
    }

But in reality, I sort of like that he has the Exceptions divided up to let the user know what they're doing incorrectly :thumbsup: