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

Simon Andersson
Simon Andersson
6,880 Points

Why is this code not working? Checking if char is uppercase

I'm trying to solve this challenge but when i run it it says

Bummer! "mFirstName" should have passed, but "Make sure the first letter is m and the second is uppercase." was reported.

What do I need to change?

TeacherAssistant.java
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    // 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


     if(fieldName.indexOf(0) != 'm' || Character.isUpperCase(fieldName.indexOf(1))) {
       throw new IllegalArgumentException("Make sure the first letter is m and the second is uppercase.");
     }
    return fieldName;

  }

}
Cindy Lea
Cindy Lea
Courses Plus Student 6,497 Points

Should the || be replaced with an "and" logical operator? You have 'or' but statement is saying 'and'

Simon Andersson
Simon Andersson
6,880 Points

I don't think so since I'm saying If any of these requirements are not met, throw the exception and exit.

However, if I would to reverse it and say "If this AND this requirement is met, return fieldName", and then make and else statement for the throw exception

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

You need to use charAt(), not index of. You also want to throw the exception when it's not uppercase so the if statement should be:

if(fieldName.charAt(0) != 'm' || !Character.isUpperCase(fieldName.charAt(1)))
Simon Andersson
Simon Andersson
6,880 Points

Ah yes, finally. Thank you. I changed to charAt instead of indexOf and now it works, and yes I had missed the exclamation mark as well. Thanks a bunch. =)