Java,使用 Scanner 尝试捕获
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32592922/
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
Java, try-catch with Scanner
提问by pavithra
I am creating a small algorithm and this is a part of it.
我正在创建一个小算法,这是其中的一部分。
If the user enters non integer values, I want to output a message and let the user enter a number again:
如果用户输入非整数值,我想输出一条消息并让用户再次输入一个数字:
boolean wenttocatch;
do
{
try
{
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
}
catch (Exception e)
{
wenttocatch=true;
System.out.println("xx");
}
} while (wenttocatch==true);
I am getting a never ending loop and I can't figure out why.
我遇到了一个永无止境的循环,我不知道为什么。
How can I identify if the user enters some non integer number?
If the user enters a non integer number, how can I ask the user to enter again?
如何识别用户是否输入了一些非整数?
如果用户输入一个非整数,我如何要求用户再次输入?
Update
When I am printing the exception I get 'InputMismatchException', what should I do?
更新
当我打印异常时,我收到“InputMismatchException”,我该怎么办?
采纳答案by Yassine.b
You dont have to do a try catch. This code will do the trick for you :
您不必尝试尝试。此代码将为您解决问题:
public static void main(String[] args) {
boolean wenttocatch = false;
Scanner scan = new Scanner(System.in);
int number_of_rigons = 0;
do{
System.out.print("Enter a number : ");
if(scan.hasNextInt()){
number_of_rigons = scan.nextInt();
wenttocatch = true;
}else{
scan.nextLine();
System.out.println("Enter a valid Integer value");
}
}while(!wenttocatch);
}
回答by James Jithin
The Scanner
does not advance until the item is being read. This is mentioned in Scanner JavaDoc. Hence, you may just read the value off using .next()
method or check if hasInt()
before reading int
value.
在Scanner
不前进,直到该项目被读取。这在Scanner JavaDoc 中提到。因此,您可以使用.next()
方法hasInt()
读取int
值或在读取值之前检查是否。
boolean wenttocatch;
int number_of_rigons = 0;
Scanner sc = new Scanner(System.in);
do {
try {
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
} catch (InputMismatchException e) {
sc.next();
wenttocatch = true;
System.out.println("xx");
}
} while (wenttocatch == true);
回答by Django
Anytime you get an exception, wenttocatch
is set to true
and the program will be stuck in an infinite loop. As long as you don't get the exception you'll not get an infinite loop.
任何时候遇到异常,wenttocatch
设置为true
并且程序将陷入无限循环。只要你没有得到异常,你就不会得到无限循环。
回答by applecrusher
The logic if sc.nextInt() causing the error is this
如果 sc.nextInt() 导致错误的逻辑是这样的
1) wenttocatch is set to false
1)gotocatch 设置为 false
2) sc.nextInt() throws error
2) sc.nextInt() 抛出错误
3) wenttocatch is set to true
3)gotocatch设置为true
4) repeat[because wenttocatch is true]
4) 重复[因为gottocatch 是真的]
To solve this set wentocatch=false in catch statement
在catch语句中解决这个set goocatch=false
catch (Exception e) {
wenttocatch=false;
System.out.println("xx");
}
if you are doing more than you show here, use a counter[if your counting or a boolean if you are not], but unless you are doing more, do the first thing above
如果你做的比你在这里展示的多,使用一个计数器[如果你在计数或布尔值如果你不是],但除非你做的更多,做上面的第一件事
boolean wenttocatch;
int count = 0;
do{
try {
wenttocatch=false;
number_of_rigons=sc.nextInt(); // sc is an object of scanner class
} catch (Exception e) {
count++;
wenttocatch=true;
System.out.println("xx");
}
}while(wenttocatch==true && count < 1);
Answer Comment:
回复评论:
I think you want to get ints until a user doesn't enter anymore. Depending on your input one way of doing that is this
我认为您想获得整数,直到用户不再输入。根据您的输入,一种方法是这样的
int number_of_rigons;
while((number_of_rigons = sc.nextInt()) != null){
//do something
}
回答by Dimang Chou
....
try {
....
} catch (Exception e) {
sc.next();
wenttocatch=true;
System.out.println("xx");
}