0

Getting the following error, trying to use an outside method that I made to initialize a bunch of coordinate pairs I'm making for a battleship terminal reproduction. I have added hasNextInt() as a test before pulling the input in as someone mentioned in another thread: https://stackoverflow.com/questions/12832006/scanner-error-with-nextint I'm getting an infinite loop when it goes into the second call of the method. though I don't have any issues if the second call to the method is commented out. My thought is its somehow taking the whitespace from the new line when I hit enter on the first coordinate pair, but I'm not sure how to clear that before it goes into the second method call so that I can actually enter values. I initialize coordinates to 6 here to make sure that only valid coordinates are entered, which triggers the infinite loop. I also have initialized to 0, and thats when I get the exception here.

The Error:

Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at Battleship.battleship.shipLocations(battleship.java:31)
        at Battleship.battleship.main(battleship.java:14)
    public static void main(String[] args){
        Scanner input = new Scanner(System.in); //read keyboard input
        //Begin the game
        System.out.println("Hello Mortals, Welcome to Battleship!");
        
        System.out.println("PLAYER 1, Enter your ships' coordinates.");
        //Take in player ones coordinates
        int[] coordinate1 = shipLocations(1);
//heres where the problem starts
        int[] coordinate2 = shipLocations(2);
        // int[] coordinate3 = shipLocations(3);
        // int[] coordinate4 = shipLocations(4);
        // int[] coordinate5 = shipLocations(5);

    
    input.close();
    }
    public static int[] shipLocations(int shipNumber){
        Scanner input = new Scanner(System.in);
        //I can either initialize over my input threshold check, to make sure only valid coordinates are available, or if I set to 0 it throws the exception error on the second method call. 
        int coordinate1=6;
        int coordinate2=6;
        System.out.println("Enter Ship " + shipNumber + " location: ");
        
        if(input.hasNextInt()){
            coordinate1 =input.nextInt();
            coordinate2= input.nextInt();
        }
        int[] coordinates = new int[2];
        if(coordinate1 > 4 || coordinate2 > 4){
            Boolean invalidCoords = true;
            do{
                System.out.println("Invalid Coordinates. Choose different Coordinates.");    
                System.out.println("Enter Ship " + shipNumber + " location: ");
                if(input.hasNextInt()){
                coordinate1 =input.nextInt();
                coordinate2 = input.nextInt();
                }
                if (coordinate1 < 5 && coordinate2 < 5){
                    invalidCoords=false;
                    coordinates[0]=coordinate1;
                    coordinates[1]=coordinate2;
                }
            }while(invalidCoords);
        }
        else{
            coordinates[0]=coordinate1;
            coordinates[1]=coordinate2;
        }
        input.close();
        return coordinates;
    }

1 Answers1

0

Ah. For anyone wondering, it was the dang input.close(). I'm not entirely sure why you can't create a new scanner on each pass, but that was mentioned in other threads that you don't want to close it so I tried removing that from the method shipLocations.