Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanner line not available error

Tags:

java

I call the Scanner.nextLine() method from two different scanner objects at two different methods. Sometimes when I call the Scanner.nextLine() from the second method, it gives me an "line not available" error. What might be the problem?

import java.util.Scanner;

public class TicTacToe {

    private final String COMPUTER = "computer";
    private final String PLAYER = "player";

    /**
     * Asks the user to choose a letter ('X' or 'O')
     * and checks if the input is valid.
     */
    public void askLetter() {
        System.out.println("Please enter 'X' or 'O': ");
        Scanner input = new Scanner(System.in);
        String letter = input.nextLine().toUpperCase().trim();
        while (!letter.equals("X") && !letter.equals("O")) {
            System.out.println("Please enter 'X' or 'O': ");
            letter = input.nextLine().toUpperCase().trim();
        }
        input.close();
    }


    /**
     * Asks the user to choose who goes first. 
     * @return 'computer' or 'player'
     */
    public String decide() {
        //System.out.println("Choose who goes first('player' or 'computer'): ");
        Scanner input = new Scanner(System.in);
        String first = input.nextLine().toUpperCase().trim();
        while (!first.equals(COMPUTER) && !first.equals(PLAYER)) {
            System.out.println("Choose who goes first('player' or 'computer'): ");
            first = input.nextLine().toUpperCase().trim();
        }
        input.close();
        return first;
    }

    public static void main(String[] args) {
        TicTacToe tictac = new TicTacToe();
        tictac.askLetter();
        tictac.decide();
    }
}
like image 916
stensootla Avatar asked Dec 26 '22 03:12

stensootla


1 Answers

Your askLetter method closes the scanner... which will close System.in. You're then trying to reuse System.in within decide.

Change the constructor to accept a Scanner, which you create in main. Then retain that scanner in a field and use it in both methods:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    TicTacToe tictac = new TicTacToe(scanner);
    tictac.askLetter();
    tictac.decide();
    // You might want to close the scanner here, but you don't really have to.
}

This also helps make your code more testable - you could now use a Scanner which is backed by fake data (e.g. a StringReader).

like image 189
Jon Skeet Avatar answered Dec 28 '22 18:12

Jon Skeet