Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

What am i doing wrong ???

I'm stuck in the java validation code challenge could you please tell me what i am doing wrong????

her's my code:

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    char [] myArray = fieldName.toCharArray();
    if(myArray[0] != 'm'){
      throw new IllegalArgumentException ("The first letter must be an m !!!!");
    }else{
      if(myArray[1] == myArray[1].toUpperCase()){
            return fieldName;
      }else{
            throw new IllegalArgumentException ("The second letter must be uppercase !!!!");
      }
    }
    // 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
  }

}

my error console tells me this error:

./TeacherAssistant.java:8: error: char cannot be dereferenced if(myArray[1] == myArray[1].toUpperCase()){ ^ 1 error

sorry i don't no how to add a picture ;(

Moderator Edited to add markdown to the code

4 Answers

Brandon Adams
Brandon Adams
10,325 Points

I think you have to use .equals() instead of == in the code. For the != use something like if(!x.equals(y))

it should be something like this:

if((!fieldName.charAt(0).equals('m'))  || Character.isLowerCase(fieldName.charAt(1)))
   throw new IllegalArgumentException ("Field Name is invalid!");
else
     return fieldName;

I had to edit my code- I didn't realize at first that it said that it had to have an 'm' at the beginning.

I had to open my workspace to check it: the code will work like this:

public class javaExample {

  public static String validatedFieldName(String fieldName) {



      if((fieldName.charAt(0) != 'm')  || Character.isLowerCase(fieldName.charAt(1)))
   throw new IllegalArgumentException ("Field Name is invalid!");
else
     return 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
  }
Martin Krstevski
Martin Krstevski
5,171 Points

In the if statement where you check if the letter is upper cased you should state as Character.toUpperCase(myArray[1]) It should look like this if(myArray[1] == Character.toUpperCase(myArray[1]))