0

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
4
  • 2
    An array and an ArrayList are different things Commented Mar 14, 2016 at 14:52
  • 1
    setLocationCells takes an ArrayList but you are passing an array. Commented Mar 14, 2016 at 14:52
  • You are trying to set the List = "String" (or similar) while the "String" is not a List, so can not be assigned to it, only added to it as an element, using the .add("String") method Commented Mar 14, 2016 at 14:54
  • try : Arrays.asList(locations) Commented Mar 14, 2016 at 14:55

2 Answers 2

2

You are getting the error because setLocationCells() method accepts an ArrayList<String> and you are passing it a String Array by doing:

dot.setLocationCells(locations);

You should either replace your method to accept String[] instead of ArrayList<String> or change your code as follows:

dot.setLocationCells(new ArrayList<String>(Arrays.asList(locations));
Sign up to request clarification or add additional context in comments.

2 Comments

You don't actually need the String - the type can be inferred by the diamond operator.
@Andy Turner Yes, I agree. new ArrayList<>(...) will work. Thanks for pointing that out.
0

You cannot have String[] locations={"2","3","4"}; and then parse it to the method setLocationCells(ArrayList<String> locs){ that requires the ArrayList.

So, there are more ways:

  1. Convert the array to list with: new ArrayList<String>(Arrays.asList(locations);
  2. Define the ArrayList instead:

    ArrayList<String> list = new ArrayList<String>() {{
        add("2");
        add("3");
        add("4");
    }};
    
  3. Change your method at all:

    public void setLocationCells(String[] locs){
        Collections.addAll(locationcells, locs);
    }
    

3 Comments

3. Not quite. Although the simple class name of the return value of Arrays.asList is ArrayList, it's actually java.util.Arrays.ArrayList rather than java.util.ArrayList. You'd need to copy it into a new ArrayList, as in 1.
2. That's not valid Java.
I have tried change my method to: public void setLocationCells(String[] locs){ locationCells=Arrays.asList(locs); } but now getting cant find symbol ^Variable Arrays error

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.