Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing an array index to a String?

Tags:

java

arrays

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"))
like image 309
user1861156 Avatar asked Dec 10 '25 20:12

user1861156


1 Answers

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.

like image 122
Óscar López Avatar answered Dec 12 '25 10:12

Óscar López