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

Expected "m_first_name" to fail but passed, but i already checked if the char is letter or not. still gives me the error

As the title says guys, i need some help and explanation if you got time. I'm having a hard time since i stopped programming for more than 1 year after graduating. Shame on me.

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.charAt(0) != 'm'){
      throw new IllegalArgumentException("All members fieldname must start with lower case letter m."); 
    }
    if(Character.isLowerCase(fieldName.charAt(1)) || Character.isLetter(fieldName.charAt(1))){
      throw new IllegalArgumentException("Second letter in fieldname should be a letter and should be camel-cased.");
    }
    return fieldName;


  }

}

2 Answers

NVM guys. got it! i just forgot the ! sign XD

 if(Character.isLowerCase(fieldName.charAt(1)) || !Character.isLetter(fieldName.charAt(1))){
      throw new IllegalArgumentException("Second letter in fieldname should be a letter and should be camel-cased.");
    }

instead of Character.isLetter(fieldName.charAt(1)) should be !Character.isLetter(fieldName.charAt(1))

Hi there,

I think the solution can be tidied up a little - there are only two tests (first letter is 'm' and second character is upper case) needed and they both must pass:

public static String validatedFieldName(String fieldName) {

  if(fieldName.charAt(0) == 'm' && Character.isUpperCase(fieldName.charAt(1))) {
     // both things are true - do whatever success looks like
  } else {
     throw new IllegalArgumentException("This does not meet the requirements!");
  }
return fieldName;
}

I hope that makes sense.

Steve.