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

Alex Jackson
PLUS
Alex Jackson
Courses Plus Student 1,352 Points

Don't know what I'm doing wrong

Comes up with 'Expected "firstName" to fail, but passed

TeacherAssistant.java
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    boolean isValidFieldName = true; 
    while (isValidFieldName != true) {
      if (fieldName.charAt(0) == 'm' || Character.isUpperCase(fieldName.charAt(1))) {
        isValidFieldName = true;
      } else {
        throw new IllegalArgumentException("Please insert either an 'm' before your field name, or capitalize the first letter of your name.");
      }
    }
      return fieldName;
    }
  }

1 Answer

Hi Alex,

You are pretty close.

A while loop is not needed here since we are just testing a single string.

Also, your if statement condition will need a boolean AND (&&) to ensure that both conditions are met.

If the condition is true, just return the fieldname. If false, throw the exception.

  if (fieldName.charAt(0) == 'm' && Character.isUpperCase(fieldName.charAt(1))) {
        return fieldName;
   }  
   else {
     throw new IllegalArgumentException("Please insert either an 'm' before your field name, or capitalize the first letter of your name.");
   }