如何检查用户输入是否不是int值
我需要检查用户输入值是否不是int值。 我已经尝试了我所知道的不同组合,但我得到的都没有或随机错误
例如:
如果用户输入“adfadf 1324”,它将发出警告信息。
是)我有的:
// Initialize a Scanner to read input from the command line Scanner sc = new Scanner(System.in); int integer, smallest = 0, input; boolean error = false; System.out.print("Enter an integer between 1-100: "); range = sc.nextInt(); if(!sc.hasNextInt()) { error = true; System.out.println("Invalid input!"); System.out.print("How many integers shall we compare? (Enter an integer between 1-100: "); sc.next(); } while(error) { for(int ii = 1; ii <= integer; ii++) { ... } // end for loop } System.out.println("The smallest number entered was: " + smallest); } }
如果输入无效,只需抛出exception
Scanner sc=new Scanner(System.in); try { System.out.println("Please input an integer"); //nextInt will throw InputMismatchException //if the next token does not match the Integer //regular expression, or is out of range int usrInput=sc.nextInt(); } catch(InputMismatchException exception) { //Print "This is not an integer" //when user put other than integer System.out.println("This is not an integer"); }
Try this one: for (;;) { if (!sc.hasNextInt()) { System.out.println(" enter only integers!: "); sc.next(); // discard continue; } choose = sc.nextInt(); if (choose >= 0) { System.out.print("no problem with input"); } else { System.out.print("invalid inputs"); } break; }
你有以下错误,这反过来导致你的exception,让我解释一下
这是您现有的代码:
if(!scan.hasNextInt()) { System.out.println("Invalid input!"); System.out.print("Enter an integer: "); usrInput= sc.nextInt(); }
在上面的代码中, if(!scan.hasNextInt())
只有当用户输入包含字符和整数(如输入adfd 123
if(!scan.hasNextInt())
才会变为true
。
但是你试图使用usrInput= sc.nextInt();
在if条件中只读取整数usrInput= sc.nextInt();
。 这是不正确的,这就是Exception in thread "main" java.util.InputMismatchException
抛出Exception in thread "main" java.util.InputMismatchException
的原因。
所以正确的代码应该是
if(!scan.hasNextInt()) { System.out.println("Invalid input!"); System.out.print("Enter an integer: "); sc.next(); continue; }
在上面的代码中, sc.next()
将有助于从用户读取新输入,并continue
将有助于再次执行相同的if条件( ie if(!scan.hasNextInt())
)。
请在我的第一个答案中使用代码来构建完整的逻辑。如果您需要任何解释,我知道。
试试这段代码[更新] :
Scanner scan = null; int range, smallest = 0, input; for(;;){ boolean error=false; scan = new Scanner(System.in); System.out.print("Enter an integer between 1-100: "); if(!scan.hasNextInt()) { System.out.println("Invalid input!"); continue; } range = scan.nextInt(); if(range < 1) { System.out.println("Invalid input!"); error=true; } if(error) { //do nothing } else { break; } } for(int ii = 1; ii <= range; ii++) { scan = new Scanner(System.in); System.out.print("Enter value " + ii + ": "); if(!scan.hasNextInt()) { System.out.println("Invalid input!"); ii--; continue; } }
摘自相关post :
public static boolean isInteger(String s) { try { Integer.parseInt(s); } catch(NumberFormatException e) { return false; } // only got here if we didn't return false return true; }
也许你可以试试这个:
int function(){ Scanner input = new Scanner(System.in); System.out.print("Enter an integer between 1-100: "); int range; while(true){ if(input.hasNextInt()){ range = input.nextInt(); if(0<=range && range <= 100) break; else continue; } input.nextLine(); //Comsume the garbage value System.out.println("Enter an integer between 1-100:"); } return range; }
这是为了在输入为整数时继续请求输入,并查找它是否为奇数,否则它将结束。
int counter = 1; System.out.println("Enter a number:"); Scanner OddInput = new Scanner(System.in); while(OddInput.hasNextInt()){ int Num = OddInput.nextInt(); if (Num %2==0){ System.out.println("Number " + Num + " is Even"); System.out.println("Enter a number:"); } else { System.out.println("Number " + Num + " is Odd"); System.out.println("Enter a number:"); } } System.out.println("Program Ended"); }
- 将Apache Shiro Security Library与基于Dropwizard的JAX-RS应用程序集成
- 使用Spring 3进行基于注释的事务管理和对象映射的最小Hibernate 4 XML配置?
- 使用Nashorn编写Java脚本(JSR 223)和预编译
- 使用TaskExecutor时数据插入问题
- 在定义字段之前无法引用字段,但仅限于您不符合条件
- 关闭应用程序时调用Java Finalize方法
- Java等待和notifyAll:IllegalMonitorStateException
- Javac vs Java in -classpath选项
- 动态类重新加载仅在调试模式下工作,为什么/如何真正起作用?