Scanner method opened and closed twice

2020-02-06 04:25发布

In a single class I have two different methods who are using the scanner class. I created a new instance of scanner object for the first method, then closed it at the end of the first method...then created a new instance of an object (with a different name) in the second method to finally close it at the end of this method.

Unless I open the scanner once and close it once it won't work and return an error. The double use of the scanner class doesn't seem to work. What am I doing wrong?

Here are my two methods which return an error...

public void GameSetup(){
    //intro to the program and what the user should expect to do
    Scanner in = new Scanner(System.in);
    out.println("Let's play a little baseball game. Type down the scores for each team at the end of each inning to determine the winner!" +
            "\nFor each inning enter the score for the first team, hit space and enter the score for the second team.\nFirst, let's enter the team names:");

    //Console will ask for the team names.
    out.println("What is the name of team 1?");
    team1Name = in.nextLine(); //Read team name 1
    out.println("What is the name of team 2?");
    team2Name = in.nextLine(); //Read team name 2
    in.close();//close the scanner
}


public void GetScores() {//this method will make the console ask for the scores for each innings

    Scanner input = new Scanner(System.in);
    out.println("What is the score at the end of 1st inning? (team 1 score <space> team 2 score)"); //The console asks to enter scores for each team
    team1Array[0] = input.nextInt(); //Read an integer for team 1 inning 1
    team2Array[0] = input.nextInt(); //Read an integer for team 2 inning 1
    out.println("What is the score at the end of 2nd inning? (team 1 score <space> team 2 score)"); 
    input.close();
}

2条回答
该账号已被封号
2楼-- · 2020-02-06 04:54

As both Scanner instances use the same InputStream source, when the first instance is closed, then the second instance is unable to read from the InputStream resulting in a NoSuchElementException.

public class GameClass {
    private final Scanner input;

    public GameClass() {
         input = new Scanner(System.in);
    }

    public void getScores() {
         team1Array[0] = input.nextInt();
         ...
    }
}

There's no need to close the Scanner instances unless you wish it to fail for subsequent reads. You could use a single instance of Scanner to avoid the overhead of 2 instances. By not closing the Scanner you also improve testability of the class.

查看更多
Animai°情兽
3楼-- · 2020-02-06 04:57

You should declare Scanner as a class variable because you're already closing the scanner in the method gameSetup(). example:

 public class Game{
        Scanner in = new Scanner(System.in);

        public void GameSetup(){
            //insert code here DO NOT CLOSE SCANNER....
        }


        public void GetScores() {
            //your code here.........
            in.close();
        }
 }

Or else what you can do is declare a scanner in each method and close in scope.

查看更多
登录 后发表回答