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

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Analysing String firstName with charAt() method

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {

    if(!Character.isUpperCase(fieldName.charAt(1))){
      throw new IllegalArgumentException("second char is not uppercase ");
    }
    if(fieldName.charAt(0) != m){
      throw new IllegalArgumentException("first char is not right");
    }
    return fieldName;
  }

}

This code works fine. But i donΒ΄t understand why we use a method from a Character wrapper class to analyse a String ???

2 Answers

The reason we use the Character wrapper class is not to analyze the string, but to analyze the characters in the String. There are no methods available, other than in the Character class, that will let us determine whether or not a specific letter in a String is uppercase or lowercase. For us to be able to do this, we must utilize another class to help, which is the Character class.

I hope this clears things up. Happy coding!

Iain Diamond
Iain Diamond
29,379 Points
fieldName.charAt(1)

Hi Grigorij,

The charAt() method takes a String type and returns a Character type, so using Character class methods to test the return type would seem best.

Hope this helps. iain

p.s.

should the second test be?

    if(fieldName.charAt(0) != 'm'){