Java 如何使用Try/Catch防止需要Int的字符串输入
我试图阻止字符串输入,并允许用户以int形式重新输入。我似乎无法让它正常工作,我在没有while循环的情况下也这样做了。我认为我对Try失配函数如何工作的理解导致了一些问题 谢谢你的帮助Java 如何使用Try/Catch防止需要Int的字符串输入,java,try-catch,Java,Try Catch,我试图阻止字符串输入,并允许用户以int形式重新输入。我似乎无法让它正常工作,我在没有while循环的情况下也这样做了。我认为我对Try失配函数如何工作的理解导致了一些问题 谢谢你的帮助 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the te
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package freetime;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author Andy
*/
public class NewClass {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int userInput=0,notInt=0,newInput=0;
System.out.println("Enter a integer number");
while (notInt == 0){
try {
newInput=scan.nextInt();
userInput=newInput;
if (userInput != newInput){
notInt=0;
}
if (userInput == newInput){
notInt=1;
}
}
catch(InputMismatchException e){
System.out.println("That is not an integer, please try again." );
}
}
System.out.println(userInput);
}
}
所以我把代码改成了这个,但我仍然在循环中运行,继续在输入int以外的内容之后运行。我做错了什么
Scanner scan=new Scanner(System.in);
int userInput=0,notInt=0,newInput=0;
boolean runL=true;
while (runL){
System.out.println("Enter a integer number");
if (scan.hasNextInt()){
userInput=scan.nextInt();
runL=false;
}
else {
System.out.println("That is not an integer, please try again.");
}
}
System.out.println(userInput);
我发现了这个问题,我需要在While循环中创建scanner对象。谢谢你的帮助 在try块中使用Integer.parseInt(输入)
。
如果输入不是int,那么它将抛出异常并在try块中的catch{}块中捕获异常。
如果输入不是int,那么它将抛出异常并在catch{}块中捕获异常
您的while循环应该如下所示:
int userInput=0,notInt=0,newInput=0;
boolean runL=true;
while (runL){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a integer number");
if (scan.hasNextInt()){
userInput=scan.nextInt();
runL=false;
}
else {
System.out.println("That is not an integer, please try again.");
}
}
System.out.println(userInput);
while (notInt == 0){
String line = scan.nextLine();
Pattern p = Pattern.compile("-?\\d+");
if (p.matcher(line).matches()) {
userInput = Integer.parseInt(line);
notInt = 1;
} else {
System.out.println("Please try again");
}
}
您的代码无法工作,因为当抛出inputmaschException
时,导致异常的令牌实际上没有被读取。所以循环每次迭代都读取相同的内容
nextLine
但是,几乎总是将整行作为字符串读取(没有行时除外)。将读取的字符串放入Integer.parseInt
以将其转换为int
。如果无法将其转换为int
,将抛出NumberFormatException
但是,您不应该使用异常作为验证输入的方法,因为它很慢。您可以使用regex尝试这种方法:
while (notInt == 0){
try {
newInput=Integer.parseInt(scan.nextLine());
userInput=newInput;
notInt=1;
}
catch(NumberFormatException e){
System.out.println("That is not an integer, please try again." );
}
}
您的while循环应该如下所示:
int userInput=0,notInt=0,newInput=0;
boolean runL=true;
while (runL){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a integer number");
if (scan.hasNextInt()){
userInput=scan.nextInt();
runL=false;
}
else {
System.out.println("That is not an integer, please try again.");
}
}
System.out.println(userInput);
while (notInt == 0){
String line = scan.nextLine();
Pattern p = Pattern.compile("-?\\d+");
if (p.matcher(line).matches()) {
userInput = Integer.parseInt(line);
notInt = 1;
} else {
System.out.println("Please try again");
}
}
您的代码无法工作,因为当抛出inputmaschException
时,导致异常的令牌实际上没有被读取。所以循环每次迭代都读取相同的内容
nextLine
但是,几乎总是将整行作为字符串读取(没有行时除外)。将读取的字符串放入Integer.parseInt
以将其转换为int
。如果无法将其转换为int
,将抛出NumberFormatException
但是,您不应该使用异常作为验证输入的方法,因为它很慢。您可以使用regex尝试这种方法:
while (notInt == 0){
try {
newInput=Integer.parseInt(scan.nextLine());
userInput=newInput;
notInt=1;
}
catch(NumberFormatException e){
System.out.println("That is not an integer, please try again." );
}
}
我认为我对Try失配函数如何工作的理解导致了一些问题
你可能就在那里
当我从您的try
/catch
语句中删除一些“奇怪”代码时,它看起来如下所示:
int userInput=0,notInt=0,newInput=0;
boolean runL=true;
while (runL){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a integer number");
if (scan.hasNextInt()){
userInput=scan.nextInt();
runL=false;
}
else {
System.out.println("That is not an integer, please try again.");
}
}
System.out.println(userInput);
while (notInt == 0){
String line = scan.nextLine();
Pattern p = Pattern.compile("-?\\d+");
if (p.matcher(line).matches()) {
userInput = Integer.parseInt(line);
notInt = 1;
} else {
System.out.println("Please try again");
}
}
这实际上意味着:
调用scan.nextInt()
如果调用成功(即未引发异常),则:
将结果分配给newInput
“做事”
如果调用失败,并抛出一个InputMismatchException
,则:
“做其他事情”
因此,基于此,您不需要在“do stuff”中做任何事情来测试您是否得到了有效的输入。你知道情况就是这样
同样,在没有得到有效输入的情况下需要做的任何事情都应该在“做其他事情”中
话虽如此,Jon Skeet关于查看hashNextInt
方法的建议是正确的。如果你使用正确的方法,你将不会得到任何异常。代码会简单得多
Jon还正确地指出,您应该使用布尔类型作为真/假逻辑。(这就是该类型的用途!)
我认为我对Try失配函数如何工作的理解导致了一些问题
你可能就在那里
当我从您的try
/catch
语句中删除一些“奇怪”代码时,它看起来如下所示:
int userInput=0,notInt=0,newInput=0;
boolean runL=true;
while (runL){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a integer number");
if (scan.hasNextInt()){
userInput=scan.nextInt();
runL=false;
}
else {
System.out.println("That is not an integer, please try again.");
}
}
System.out.println(userInput);
while (notInt == 0){
String line = scan.nextLine();
Pattern p = Pattern.compile("-?\\d+");
if (p.matcher(line).matches()) {
userInput = Integer.parseInt(line);
notInt = 1;
} else {
System.out.println("Please try again");
}
}
这实际上意味着:
调用scan.nextInt()
如果调用成功(即未引发异常),则:
将结果分配给newInput
“做事”
如果调用失败,并抛出一个InputMismatchException
,则:
“做其他事情”
因此,基于此,您不需要在“do stuff”中做任何事情来测试您是否得到了有效的输入。你知道情况就是这样
同样,在没有得到有效输入的情况下需要做的任何事情都应该在“做其他事情”中
话虽如此,Jon Skeet关于查看hashNextInt
方法的建议是正确的。如果你使用正确的方法,你将不会得到任何异常。代码会简单得多
Jon还正确地指出,您应该使用布尔类型作为真/假逻辑。(这就是该类型的用途!)您是否看到了hasnetint
方法?(我还强烈建议您使用布尔类型来表示真
/假
值,而不是使用值为0或1的int
)如果条件在代码中没有用处,请与自身进行比较并使用上述建议您是否看到了hasnetint
方法?(我还强烈建议您对true
/false
值使用boolean
类型,而不是使用值为0或1的int
)如果条件在代码中没有用处,请与自身进行比较并使用上述建议您无法使用正则表达式(可靠地)进行验证。与该模式匹配的有效整数仍将引发异常。(提示:2^31-1不是10的幂……)@StephenC我明白了。如果用户输入的数字超过整数的最大值,它会崩溃,不是吗?如果这样大的输入在上下文中有意义,OP可能应该使用long或biginger
,而不是int<