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

Michael LaCroix
Michael LaCroix
5,828 Points

Struggling to verify capitalization of specific character

I need to verify the capitalization of the second character seeing as it's a member variable.

I'm pretty sure I need to use either some form of not upper case (! = isUpperCase) or throwing the exception if I find a lower case (isLowerCase). I also need to specifically check the second character with (charAt(1)), I think. I can't figure out how to combine these methods. I've tried a number of variations and nothing seems to pan out. Thoughts?

TeacherAssistant.java
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
     if (fieldName.charAt(0) != 'm' || fieldName.isLowerCase(charAt(1)) {
        throw new IllegalArgumentException("Field must begin with 'm' and second letter must be capitalized.");
    }
    // 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

Cristian Altin
Cristian Altin
12,165 Points

Try Character.isLowerCase(fieldName.charAt(1)) instead

Michael LaCroix
Michael LaCroix
5,828 Points

Thank you!!!! Didn't know I need the Character class. Still not really sure how all this fits together, but I guess that's part of learning:)

!Character.isUpperCase(fieldName.charAt(1)) is what ended up working, but you gave me the clue I needed.

Cristian Altin
Cristian Altin
12,165 Points

Glad to be of help. You basically negated the UpperCase doing the same as my answer, but maybe the exercise was expecting a not expression. If you remember to upvote or mark my answer it would be nice.