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

Kevin McFarland
Kevin McFarland
5,122 Points

Stage 4 "Delivering the MVP Java Objects Error - Bummer! Expected "firstName" to fail but it passed.

Not sure what to think about this challenge - help!

TeacherAssistant.java
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    char first;
    char second;
    first = fieldName.charAt(0) ;
    second = fieldName.charAt(1) ;    


    boolean isValidFirstChar = (first == 'm');
    boolean isValidSecondChar = (Character.isUpperCase(second));
    try 
        {
           if (isValidFirstChar && isValidSecondChar);

        }

    catch (IllegalArgumentException iae)
        {
             throw new IllegalArgumentException("IllegalExcepttion");
        }  

    // 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
    return fieldName;
  }

}

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

So close Kevin! Leave the trying and catching to the caller of the method (not your job). Regarding your if statement. ..You could have two separate if statements negate each value. Or you could surround the statement you built with parenthesis and negate it. I've included that below since that is a new concept. Code will execute in the parens and then you negate it's value.

if (! (isValidFirstChar && isValidSecondChar)) {
    throw new IllegalArgumentException("Bad method name");
}

I intended two separate if statements, but kudos for going above and beyond ;)

Kevin McFarland
Kevin McFarland
5,122 Points

Hi Craig,

I know that I tend to over think the challenges.

It makes sense now... thanks

Kevin

Kaleb Burnham
Kaleb Burnham
4,877 Points

I found a much simpler way to complete this challenge, only requiring two additional lines. No need to create any variables.

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName)  {
    if (fieldName.charAt(0) != 'm' || !Character.isUpperCase(fieldName.charAt(1))) {
    throw new IllegalArgumentException("IllegalException");
  }
  return fieldName;   
  }
}