Here is a program similar to the sinking battleship game from the book head first java. After compiling I am getting the error: "String cannot be converted to ArrayList Error" and ^ pointer point to the line I have two different files one with the main method and other a separate class. Whats wrong here.
Main method class
import java.util.Scanner;
public class SimpleDotComTestDrive{
public static void main(String[] args){
SimpleDotCom dot=new SimpleDotCom();
boolean repeat=false;
String[] locations={"2","3","4"};
dot.setLocationCells(locations); //^ where compiler points the error
Scanner input=new Scanner(System.in);
System.out.println("Lets Start");
while(repeat==false) {
System.out.println("Type your guess");
String userGuess=input.nextLine();
String result=dot.checkYourSelf(userGuess);
System.out.println(result);
if(result=="kill") {
repeat=true;
break;
}
}
} //close main
} //close test class
Separately saved class which is part of this program:
import java.util.ArrayList;
public class SimpleDotCom {
private ArrayList<String>locationCells;
public void setLocationCells(ArrayList<String> locs) {
locationCells=locs;
}
public String checkYourSelf(String userGuess) {
String result="miss";
int index = locationCells.indexOf(userGuess);
if(index>=0) {
locationCells.remove(index);
if(locationCells.isEmpty()) {
result="kill";
}
else {
result="hit";
}
}
return result;
} //close check yourself method
} //close simple class
setLocationCellstakes anArrayListbut you are passing an array.Arrays.asList(locations)