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

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