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

Sina Maleki
Sina Maleki
6,135 Points

misunderstand

I don't understand what the question is. I mean this question want me the method like "validateGuess()" method on last video or like "promptForGuess()" method.

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
      boolean bol1 = fieldName.charAt(0) != 'm';
      if (bol1) {
        throw new IllegalArgumentException("You are wrong, because you do not follow the rule for fieldName");
      }
      boolean bol2 = Character.isLowerCase(fieldName.charAt(1));
      if(bol2){
         throw new IllegalArgumentException("You are wrong, because you do not follow the rule for fieldName");
      }
      boolean bol3 = Character.isLetter(fieldName.indexOf(1));
       if(!bol3){
         throw new IllegalArgumentException("You are wrong, because you do not follow the rule for fieldName");
      }
    return fieldName;
  }
}

2 Answers

rydavim
rydavim
18,813 Points

You're headed in the right direction, but your code can be much simpler.

All you need to do is check that the two conditions are met (starts with m, followed by a capital letter). If they are true, you return the fieldname, otherwise you throw an error.

// You want to check that the first character is m and the second character is capitalized.
if (fieldName.charAt(0) == 'm' && Character.isUpperCase(fieldName.charAt(1))) {
      return fieldName;  // If those things are true, return fieldName.
    } else {
      throw new IllegalArgumentException("Fieldname is Invalid.");  // Otherwise, throw an exception.
    }
  }
Sina Maleki
Sina Maleki
6,135 Points

Hi rydavim, Thanks for your guide, my problem's been resolved.