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

Need Help

I Don't Know Why It Isn't Working

TeacherAssistant.java
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    char name = fieldName.charAt(1);
    // 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
    while(!fieldName.contains("m") && !fieldName.isTitleCase(1)){
       throw new IllegalArgumentException("Name Needs To Start With The Letter M");
    }


    return fieldName;
  }

}

2 Answers

Jordan Payne
Jordan Payne
5,478 Points
  • Think about using an "if" statement instead of a while loop.
  • Use the "if" statement to check whether to throw the exception
  • If the character at position 0 != m, throw the exception OR if the character at position 1 is not upper case, throw the exception
  • Have a look at Character.isUpperCase(char c) for the second condition.

I Changed It But I'm Not Sure Why It Wont Work? My Code:

public class TeacherAssistant {

public static String validatedFieldName(String fieldName) { if(!fieldName.contains("m") && !Character.isUpperCase(1)){

     throw new IllegalArgumentException("Name Needs To Start With The Letter M");

}
return fieldName;

}

}

Jordan Payne
Jordan Payne
5,478 Points
  • You're very close, read my third bullet point above. Note the OR instead of the AND.
  • The argument for Character.isUpperCase() is a character, not an integer. You want to feed the character at index 1 in your fieldName String to that method. Try using the .chartAt(int index) method to get the character you want.
  • fieldName.contains("m") will return true if "m" is anywhere inside the String assigned to fieldName. You could do away completely with the .contains() method and instead use .charAt(int index) to check if the character at index 0 != 'm'.