扫描仪方法打开和关闭两次

时间:2013-07-12 19:24:05

标签: java java.util.scanner

在单个班级中,我有两种使用扫描仪类的方法。我为第一个方法创建了一个新的scanner对象实例,然后在第一个方法的末尾关闭它...然后在第二个方法中创建一个对象的新实例(具有不同的名称),最后在这个方法结束了。

除非我打开扫描仪一次并关闭它,否则它将无效并返回错误。扫描仪类的双重用途似乎不起作用。我究竟做错了什么?

以下是我的两个返回错误的方法......

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();
}

3 个答案:

答案 0 :(得分:6)

由于两个Scanner实例都使用相同的InputStream源,因此当第一个实例关闭时,第二个实例无法从InputStream读取,从而导致NoSuchElementException }。

public class GameClass {
    private final Scanner input;

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

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

除非您希望后续读取失败,否则无需关闭Scanner个实例。您可以使用Scanner的单个实例来避免2个实例的开销。 通过不关闭Scanner,您还可以提高课程的可测试性。

答案 1 :(得分:0)

您应该将Scanner声明为类变量,因为您已经在方法gameSetup()中关闭了扫描程序。例如:

 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();
        }
 }

或者你可以做的是在每种方法中声明一个扫描仪并在范围内关闭。

答案 2 :(得分:0)

我学习Java已有很短的时间了,但我真的不知道我在做什么,但这对我很有用。我不知道为什么会这么做,但是...

import java.util.Scanner;
public class Inputing{

    public Scanner input_scan = null;

    public static int ScanningInput() {
        int input_value = 0;
        Scanner input_scan = new Scanner(System.in);

        try {
            input_value = input_scan.nextInt();
        } catch (Exception e) {
            System.out.println("!error!");
        } finally {
            if (input_scan.equals(null)) {
                input_scan.close();
            }
        }
        return input_value;
    }

    public static int getInputA(int scan_valueA){
        scan_valueA = (ScanningInput());
        return scan_valueA;
    }

        public static int getInputB(int scan_valueB){
        scan_valueB = (ScanningInput());
        return scan_valueB;
    }
}

// then call it with another class

public class MainClass{

    public static void main(String[] args) {

        int variableA = 0; 
        variableA = Inputing.getInputA(variableA);

        int variableB = 0;
        variableA = Inputing.getInputC(variableB);
    }

}

对于任何错误,我深表歉意,并感谢您的指导。我是一个完整的菜鸟,但是在弄乱了这段代码一段时间之后,似乎没有任何警告,并返回了我想要的结果。