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

How do I check if a character is uppercase?

Hello. I thought that since I've had such great help from the community I'll post another question. So the problem I'm encountering is that I have to check for two conditions, if the first character is 'm' and if the second character is uppercase. I'll post the decription of the exercise below in case you don't see it.

"So I was wondering if you could help me with some of these code challenges. As you might've seen when a member field name doesn't match the style we've talked about, I send an error about it. Let's write some code to validate the style of the field name.

I've added the method validatedFieldName it will return the validated field name. If the value passed in doesn't meet the requirements, throw an IllegalArgumentException. I'll keep you posted on the results every time you press check work."

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.isLowerCase(fieldName.indexOf(1)) */ ) {
     throw new IllegalArgumentException("Make sure the first charater is m and the second is uppercase.");
    }

    return fieldName;
  }

}

2 Answers

Java has an isUpperCase() function that takes a char and returns a boolean:

boolean isUpperCase(char ch)
Simon Andersson
Simon Andersson
6,880 Points

Yeah I used the isLowerCase instead and if it returns true then it throws the exception, the problem is i dont know how to write it out. Should I just write boolean isUpperCase(char ch) = false?

Simon Andersson
Simon Andersson
6,880 Points

I tried this: if(fieldName.indexOf(0) != 'm' || Character.isLowerCase(fieldName.indexOf(1)) ) {

But it says "Make sure the first letter is 'm' and the second character is uppercase."