I have a main function in which I use scanner to read an integer from console. Inside this main function, we can access another function which also uses scanner to read an integer. So, the program swings between these two functions many times. But, Java.util.scanner throws an exception. Is there any way to overcome this?
我有一个主要功能,我使用扫描仪从控制台读取整数。在这个main函数中,我们可以访问另一个也使用scanner来读取整数的函数。因此,程序在这两个函数之间多次摆动。但是,Java.util.scanner会抛出异常。有什么方法可以克服这个问题吗?
import java.util.Scanner;
public class dummy {
public static void main(String[] args) {
int buy;
Scanner sc = new Scanner(System.in);
buy = sc.nextInt();
user = dummy2();
sc.close();
}
static boolean dummy2(){
Scanner sc1 = new Scanner(System.in);
sc1.close();
}
}
3 个解决方案
#1
2
First of all, it would make the question much easier to answer if you gave more information, such as the exception and its message, and maybe source code.
首先,如果您提供了更多信息,例如异常及其消息,以及源代码,它将使问题更容易回答。
If the exception is a NoSuchElementException, the direct problem is that the function is closing the Scanner. When the scanner is closed, it also closes the underlying ImputStream. This makes all other Scanner on that input invalid.
如果异常是NoSuchElementException,则直接问题是该函数正在关闭扫描程序。当扫描仪关闭时,它也会关闭底层的ImputStream。这使得该输入上的所有其他扫描程序无效。
If the exception is InputMismatchException, then the input is not an int.
如果异常是InputMismatchException,则输入不是int。
If the exception is IllegalStateException, then the scanner has been closed, this could happen is the function and the main method are using the scanner, and one closes it.
如果异常是IllegalStateException,则扫描程序已关闭,这可能发生在函数和主方法正在使用扫描程序,并且关闭它。
However, you should not be taking user input in functions. This limits future use, say if you wanted to later add a GUI or make the same calculation based off a number not gotten from the user, then you would need rewrite the function. The function should take a int as a parameter, which the main method should get from the user. Only the main method and other methods directly relating to user input, such as the Scanner's methods, should read user input.
但是,您不应该在函数中接受用户输入。这限制了未来的使用,例如,如果您想稍后添加GUI或根据未从用户获得的数字进行相同的计算,那么您需要重写该功能。该函数应该将int作为参数,main方法应该从用户获取。只有与用户输入直接相关的主要方法和其他方法(如扫描仪的方法)才应读取用户输入。
#2
1
Use the same Scanner object.
使用相同的扫描仪对象。
import java.util.Scanner;
public class dummy {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
//Do more stuff with the same scanner
//close it when done
}
static boolean dummy2(){
//Scan stuff
int nbr = sc.nextInt();
}
#3
-1
I would suggest something like that:
我会建议这样的事情:
import java.util.Scanner;
public class dummy {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
sc.close();
}
static boolean dummy2(){
//lets scan a string.
sc.nextLine();
}
}
Reusable objects! Isn't that nice?
可重复使用的物体!不是很好吗?
#1
2
First of all, it would make the question much easier to answer if you gave more information, such as the exception and its message, and maybe source code.
首先,如果您提供了更多信息,例如异常及其消息,以及源代码,它将使问题更容易回答。
If the exception is a NoSuchElementException, the direct problem is that the function is closing the Scanner. When the scanner is closed, it also closes the underlying ImputStream. This makes all other Scanner on that input invalid.
如果异常是NoSuchElementException,则直接问题是该函数正在关闭扫描程序。当扫描仪关闭时,它也会关闭底层的ImputStream。这使得该输入上的所有其他扫描程序无效。
If the exception is InputMismatchException, then the input is not an int.
如果异常是InputMismatchException,则输入不是int。
If the exception is IllegalStateException, then the scanner has been closed, this could happen is the function and the main method are using the scanner, and one closes it.
如果异常是IllegalStateException,则扫描程序已关闭,这可能发生在函数和主方法正在使用扫描程序,并且关闭它。
However, you should not be taking user input in functions. This limits future use, say if you wanted to later add a GUI or make the same calculation based off a number not gotten from the user, then you would need rewrite the function. The function should take a int as a parameter, which the main method should get from the user. Only the main method and other methods directly relating to user input, such as the Scanner's methods, should read user input.
但是,您不应该在函数中接受用户输入。这限制了未来的使用,例如,如果您想稍后添加GUI或根据未从用户获得的数字进行相同的计算,那么您需要重写该功能。该函数应该将int作为参数,main方法应该从用户获取。只有与用户输入直接相关的主要方法和其他方法(如扫描仪的方法)才应读取用户输入。
#2
1
Use the same Scanner object.
使用相同的扫描仪对象。
import java.util.Scanner;
public class dummy {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
//Do more stuff with the same scanner
//close it when done
}
static boolean dummy2(){
//Scan stuff
int nbr = sc.nextInt();
}
#3
-1
I would suggest something like that:
我会建议这样的事情:
import java.util.Scanner;
public class dummy {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
sc.close();
}
static boolean dummy2(){
//lets scan a string.
sc.nextLine();
}
}
Reusable objects! Isn't that nice?
可重复使用的物体!不是很好吗?