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

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;   
  }
}