3

I'm trying to compare an index from my array of char's to a string, I'm using the .equals method but I'm running into errors. Any help?

if (j[1].equals("A"))
{j[1] = 10;}

I keep getting this error:

int cannot be dereferenced
else if (j[1].equals("A"))
4
  • char is a primitive data type, not an object. Commented Jan 24, 2013 at 18:53
  • Do you have an array of chars or Strings? Commented Jan 24, 2013 at 18:53
  • Relevent question stackoverflow.com/questions/767372/java-string-equals-versus Use .equals for Strings with other Strings. j[1] is not a String. Commented Jan 24, 2013 at 18:54
  • I have an array of Char's that are mixed with numbers and the Letters A to F. What I would like is to be able to say, if the Char is = to A then assign the value 10 to a new variable or the same index in the array, whatever is easier Commented Jan 24, 2013 at 18:56

3 Answers 3

3

Try this instead:

if (j[1] == 'A')

If the j array contains chars, then you must compare its values with another char, not a String (even if the string has length 1) - they're different data types. And because a char is a primitive type, == is used for equality comparisons.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that worked perfectly. I'll keep the data types in mind next time and hopefully thats one mistake I won't be making again!
@user1861156 you're welcome! please don't forget to accept the answer that was most helpful for you, by clicking on the check mark to its left
2

In order to compare single characters, you need to use the == operator because char is a primitive type and Java does not allow to call methods on primitive types. Also, you can't directly compare with a string, you need to compare to a char. Constants of type char are written with single quotes as opposed to double quotes which denote String constants.

Thus your code would be:

if (j[1] == 'A') 

You should then use another variable for storing the value 10. Although you could theoretically store it in the array (characters are just numbers, too), this would be very confusing code. Thus use an additional int variable.

Also note that in Java, you should speak of chars, not of Chars. The reason is that there is also a class name Character which also represents a single character, but as an object instead of a primitive type. Thus when you say Char, it is not clear if you mean char of Character.

1 Comment

This explanation helped a lot! Thank you, I will be keeping that in mind!
0

If j[1] is an array of char (the primitive, not the object Character) then you cannot not call the equals method on it, just like you cannot call 1.equals("A").

So, this would work instead (using objects)

    char[] array = new char[] { 'a', 'b', 'c' };
    boolean equals = array[0].equals('a'); // Cannot invoke equals(char) on
                                            // the primitive type char
    Character.valueOf('a').equals(Character.valueOf(array[0])); //This works

Or this, using primitives:

    char[] array = new char[] { 'a', 'b', 'c' };
    boolean anotherEquals = array[0] == 'a'; // This works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.