java 扫描仪方法打开和关闭两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17622556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scanner method opened and closed twice
提问by Reimeus
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();
}
采纳答案by HCL02
You should declare Scanner as a class variable because you're already closing the scanner in the method gameSetup(). example:
您应该将 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();
}
}
Or else what you can do is declare a scanner in each method and close in scope.
或者你可以做的是在每个方法中声明一个扫描器并关闭范围。
回答by Reimeus
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
.
由于两个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();
...
}
}
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.
Scanner
除非您希望它在后续读取时失败,否则无需关闭实例。您可以使用单个实例Scanner
来避免 2 个实例的开销。通过不关闭Scanner
您还可以提高类的可测试性。
回答by kurt_hilder
I have been learning Java for a short time and don't really know what I am doing, but this worked for me. I have no idea why it did but...
我一直在学习 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);
}
}
I apologize for any errors, and would appreciate guidance. I am a complete rookie but after messing with this code for a while, this seemed to produce no warnings and return the results I was looking for.
对于任何错误,我深表歉意,并希望得到指导。我是一个完整的菜鸟,但是在弄乱了这段代码一段时间后,这似乎没有产生警告并返回我正在寻找的结果。