I keep getting this error with my code. I can't seem to find the problem.
I'm not sure what to do because I even looked in the text book and it gives me a similar method except with different variables.
I'm on BlueJ.
public int findFirstOfPlayer(String searchString)
{
int index = 0;
boolean searching = true;
while(index < cards.size() && searching) {
String cardname = cards.get(index); // Error highlights index
if(cardname.contains(searchString)) {
searching = false;
}
else {
index++;
}
if(searching) {
return -1;
}
else {
return index;
}
}
}
2 个答案:
答案 0 :(得分:2)
Here is the problem, and if you used something like Eclipse or Idea, it would even highlight it for you.
String cardname = cards.get(index);
Obviously, cards is not compatible with String, you can't just assign it that way, because your collection card is probably not of type String aka ArrayList<String> cards
You can do either:
String cardname = cards.get(index).toString();
or
String cardname = String.valueOf(cards.get(index));
答案 1 :(得分:1)
Given that Card is a class on its own you can't compare it to string. You have to either implement a method that returns a string you can compare with searchString or use a variable within the card object for the comparisson.
Something like this:
String cardname = cards.get(index).toString();
Or
String cardname = cards.get(index).name