Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Validation

Eric Stermer
Eric Stermer
9,499 Points

Not sure why the test m_ would work with my current code?

I have a test to see if the first and second characters are letters and if the first character is an 'm' and the second is uppercase, but it keeps saying the test still works. I am not sure where to go from here.

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

    char[] fieldNameArray = fieldName.toCharArray();

    if (Character.isLetter(fieldNameArray[0]) && Character.isLetter(fieldNameArray[1])){
      if (fieldNameArray[0] != 'm') {
        throw new IllegalArgumentException("The first letter must be a 'm'");
      }
      if (! Character.isUpperCase(fieldNameArray[1])) {
         throw new IllegalArgumentException("The second letter uppercase");
      }
    }


    return fieldName;
  }

}

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Eric

So you've actually adding in extra checking which in this case is allowing false positives. The first if statement check to see if the first and second element of the char array are letters which fails when the test enters an underscore which results in the other if tests not being run at all. By simply removing this first check (as per below) it will correctly identify the underscore as not matching an Uppercase letter.

Good job for getting the rest of the code spot on!

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

    char[] fieldNameArray = fieldName.toCharArray();


      if (fieldNameArray[0] != 'm') {
        throw new IllegalArgumentException("The first letter must be a 'm'");
      }
      if (! Character.isUpperCase(fieldNameArray[1])) {
         throw new IllegalArgumentException("The second letter uppercase");
      }



    return fieldName;
  }

}

Hope this helps

Daniel