Java 如果用户输入字符串而不是Int,则可能出现异常
我只是在玩Java,我试图强迫我的程序只接受3位数字。 我相信我已经使用while循环成功地完成了这项工作(如果我错了,请纠正我)。 但是,如果用户输入字符串,如何打印错误语句呢。例如:“abc” 我的代码:Java 如果用户输入字符串而不是Int,则可能出现异常,java,string,loops,while-loop,int,Java,String,Loops,While Loop,Int,我只是在玩Java,我试图强迫我的程序只接受3位数字。 我相信我已经使用while循环成功地完成了这项工作(如果我错了,请纠正我)。 但是,如果用户输入字符串,如何打印错误语句呢。例如:“abc” 我的代码: import java.util.Scanner; public class DigitSum { public static void main(String[] args) { Scanner newScan = new Scanner(System
import java.util.Scanner;
public class DigitSum {
public static void main(String[] args) {
Scanner newScan = new Scanner(System.in);
System.out.println("Enter a 3 digit number: ");
int digit = newScan.nextInt();
while(digit > 1000 || digit < 100)
{
System.out.println("Error! Please enter a 3 digit number: ");
digit = newScan.nextInt();
}
System.out.println(digit);
}
}
import java.util.Scanner;
公共类数字图书馆{
公共静态void main(字符串[]args){
扫描仪新闻扫描=新扫描仪(System.in);
System.out.println(“输入一个3位数字:”);
int digit=newScan.nextInt();
而(位数>1000 | |位数<100)
{
System.out.println(“错误!请输入一个3位数字:”);
digit=newScan.nextInt();
}
系统输出打印项次(位数);
}
}
当您抓取输入或拉动输入字符串时,运行parseInt
。
如果yourString
不是整数
,这实际上会引发异常:
Integer.parseInt(您的字符串)
如果它抛出异常,您知道它不是有效的输入,所以此时您可以显示错误消息。以下是parseInt
上的文档:
)在try catch块中嵌入读取int的代码。每当输入错误时,它将生成异常,然后在catch块中显示您想要的任何消息。您可以通过以下方式检查字符串是否为数值: 1) 使用try/Catch块
try
{
double d = Double.parseDouble(str);
}catch(NumberFormatException nfe) {
System.out.println("error");
}
2) 使用正则表达式
if (!str.matches("-?\\d+(\\.\\d+)?")){
System.out.println("error");
}
3) 使用NumberFormat类
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition pos = new ParsePosition(0);
formatter.parse(str, pos);
if(str.length() != pos.getIndex()){
System.out.println("error");
}
4) 使用Char.isDigit()命令
您可以在此处查看更多信息
nextInt
方法本身在输入错误时抛出inputmaschException
try {
digit = newScan.nextInt()
} catch (InputMismatchException e) {
e.printStackTrace();
System.err.println("Entered value is not an integer");
}
这个应该可以。这个怎么样
public class Sample {
public static void main (String[] args) {
Scanner newScan = new Scanner (System.in);
System.out.println ("Enter a 3 digit number: ");
String line = newScan.nextLine ();
int digit;
while (true) {
if (line.length () == 3) {
try {
digit = Integer.parseInt (line);
break;
}
catch (NumberFormatException e) {
// do nothing.
}
}
System.out.println ("Error!(" + line + ") Please enter a 3 digit number: ");
line = newScan.nextLine ();
}
System.out.println (digit);
}
}
regexp版本:
public class Sample {
public static void main (String[] args) {
Scanner newScan = new Scanner (System.in);
System.out.println ("Enter a 3 digit number: ");
String line = newScan.nextLine ();
int digit;
while (true) {
if (Pattern.matches ("\\d{3}+", line)) {
digit = Integer.parseInt (line);
break;
}
System.out.println ("Error!(" + line + ") Please enter a 3 digit number: ");
line = newScan.nextLine ();
}
System.out.println (digit);
}
}
我将如何使用if语句。if语句应如下所示:
if(input.hasNextInt()){
// code that you want executed if the input is an integer goes in here
} else {
System.out.println ("Error message goes here. Here you can tell them that you want them to enter an integer and not a string.");
}
注意:如果希望他们输入字符串而不是整数,请更改
if语句调用input.hasNextLine()
而不是input.hasNextInt()
第二个注意事项:input
是我命名的扫描仪。如果您为您的煎饼命名,则应键入pancakes.hasNextInt()
或pancakes.hasNextLine()
希望我能帮忙,祝你好运 如果用户输入一个字符串,例如'abc'而不是整数值,则希望出现异常,则
输入不匹配异常
适合您
让我给你举个基本的例子
public static void main(String[] args)
{
Scanner ip = new Scanner(System.in);
int a;
System.out.println("Enter Some Input");
try{
a = ip.nextInt();
}
catch(InputMismatchException msg){
System.out.println("Input Mismatch Exception has occured " + msg.getMessage());
}
}
这太完美了。非常感谢。这正是我想要达到的。谢谢大家。你的答案都是正确的。不过,这个方法对我更有效:D“Integer.parseInt()”不是精华。您可以使用regexp而不是“Integer.parseInt()”。看看您的答案Ruzia,您使用了
Integer.parseInt()
我认为这是非常重要的(不管这意味着什么)。还有,既然你接受了我的答案,为什么你要否决我的答案?谁是我?我没有否决任何人。我只是理解整数。parseInt()。不确定regexp的意思是什么…>john我认为本质是while循环代码。“Integer.parseInt()”是选项之一。了解它非常方便。非常感谢。
public static void main(String[] args)
{
Scanner ip = new Scanner(System.in);
int a;
System.out.println("Enter Some Input");
try{
a = ip.nextInt();
}
catch(InputMismatchException msg){
System.out.println("Input Mismatch Exception has occured " + msg.getMessage());
}
}