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"))
user1861156
  • 169
  • 1
  • 2
  • 14
  • `char` is a primitive data type, not an object. – mre Jan 24 '13 at 18:53
  • Do you have an array of chars or Strings? – Mike Jan 24 '13 at 18:53
  • Relevent question http://stackoverflow.com/questions/767372/java-string-equals-versus Use `.equals` for Strings with other Strings. `j[1]` is not a String. – aug Jan 24 '13 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 – user1861156 Jan 24 '13 at 18:56

3 Answers3

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 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 Jan 24 '13 at 19:01
  • @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 – Óscar López Jan 24 '13 at 19:02
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.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
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
Miquel
  • 15,405
  • 8
  • 54
  • 87