0

I bought Head First Java book, and i am trying to do the exercise sink a startup.

After a while the book introduce ArrayList, and show how to use it in the class and its' method.

Problem is once i change everything with arraylist, the MAIN doesn't work, becasue at start i used simple INT, in the array location, now it need array.

How can i change the values of INT into a type that i can put inside the array ?

thx for help, and here the code.

the class with the method:

private ArrayList<String> locationCells;
private int numOfHits = 0;


public void setLocationCells(ArrayList<String> locationCells) 
{
    this.locationCells = locationCells;
}


    public String checkYourself(String guess) {
    // creazione stringa miss
        String result = "miss";
        
        int index = locationCells.indexOf(guess);
        
        if (index >= 0) {
            
            locationCells.remove(index);
        }   
    
            
            if (locationCells.isEmpty()) {
                result = "kill";
                numOfHits ++;
                                
            }else 
                result = "hit";

        System.out.println(result);
        return result;
        
    }

and here the MAIN:

public static void main(String[] args) {
        
     java.util.Random randomGenerator = new java.util.Random();
     Scanner scan = new Scanner(System.in);
    
     
     StartUpCorretta dot = new StartUpCorretta();

    
    int manyGuesses = 0;
    
    boolean isAlive = true;
    
    
    int randomNumbers = randomGenerator.nextInt(5) +1;
    
    
    int randomNumb = (int) (Math.random() * 5);
    
    
    ArrayList<String> location = {randomNumbers,randomNumbers +1,randomNumbers +2};
    
    dot.setLocationCells(location);

    while(isAlive) {
        
        System.out.println("enter a number");
        int guess = scan.nextInt();
        
        String result = dot.checkYourself(guess);
        
        manyGuesses ++ ;
        
        if (result.equals("kill")) {
            isAlive = false;
            
        System.out.println("you took" + " " + manyGuesses + " " + "guesses");
        }
    
    }
    
    
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 2
    "*`ArrayList location = {randomNumbers,randomNumbers +1,randomNumbers +2};`*" - An `ArrayList` cannot be initialized like this. For one, the types do not match (`String` != `int`), for another, the array-initialization syntax does not work for `ArrayList`s. – Turing85 Sep 24 '22 at 11:08
  • [This](https://stackoverflow.com/q/1005073/10671013) is another SO question that discusses initialization of arrayList with pre-determined values, though you would probably want to use String.valueOf() to convert those `int`s into string – Tan Yu Hau Sean Sep 24 '22 at 11:36

1 Answers1

2

Seems, you are taking your input from console as int from this statement int guess = scan.nextInt();

So, my answer is based on your input data type.

Please, changed your arraylist generic type String to Integer And Secondly, this is not how you can initialized the arraylist

ArrayList<String> location = {randomNumbers,randomNumbers +1,randomNumbers +2};

Correct way to create collection using new operator like this

ArrayList<Integer> location = new ArrayList<>();

and correct way to add element into arraylist like this

location.add(randomNumbers);
location.add(randomNumbers+1);
location.add(randomNumbers+2);

Hope, it will work for you.

Sawan Meshram
  • 230
  • 2
  • 12
  • Or if you want your `arraylist` collection as `String` type, then you need to used `String.valueOf()` to take your integer random number to String and need to changes `int guess = scan.nextInt();` to this one `String guess = scan.next();` – Sawan Meshram Sep 24 '22 at 11:54