Java scanner.nextInt(),超出范围避免?
我已经完成了一个简单的程序,可以将十进制数转换成二进制Java scanner.nextInt(),超出范围避免?,java,int,range,java.util.scanner,out,Java,Int,Range,Java.util.scanner,Out,我已经完成了一个简单的程序,可以将十进制数转换成二进制(32位)。如果用户输入一个溢出号(任何超过2147483647),我想实现某种类型的错误消息。我尝试了一个if_else,loop,但很快发现我甚至做不到。因此,我把输入当作字符串,然后使用.valueOF()等东西,但似乎仍然无法找到解决方案 如果不能首先存储值,我看不出如何将任何值与a>2147483648进行比较 下面是我为getDecimal()方法编写的简单代码: numberIn=scan.nextInt() Edit::尝试t
(32位)
。如果用户输入一个溢出号(任何超过2147483647
),我想实现某种类型的错误消息。我尝试了一个if_else,loop
,但很快发现我甚至做不到。因此,我把输入当作字符串,然后使用.valueOF()
等东西,但似乎仍然无法找到解决方案
如果不能首先存储值,我看不出如何将任何值与a>2147483648
进行比较
下面是我为getDecimal()
方法编写的简单代码:
numberIn=scan.nextInt()代码>
Edit::尝试try/catch方法后,遇到编译错误
“无法从静态上下文引用非静态方法nextInt()”
我的代码如下
public void getDec()
{
System.out.println("\nPlease enter the number to wish to convert: ");
try{
numberIn = Scanner.nextInt();
}
catch (InputMismatchException e){
System.out.println("Invalid Input for a Decimal Value");
}
}
我建议您在该语句周围加一个try/catch块,用于NumberFormatException
像这样:
try {
numberIn = Integer.valueOf(scan.next());
}catch(NumberFormatException ex) {
System.out.println("Could not parse integer or integer out of range!");
}
我建议您在该语句周围加一个try/catch块,用于NumberFormatException
像这样:
try {
numberIn = Integer.valueOf(scan.next());
}catch(NumberFormatException ex) {
System.out.println("Could not parse integer or integer out of range!");
}
如果下一个标记无法转换为int
,则可以使用返回false
的方法。然后在else
块中,您可以使用将输入读取为字符串,并打印相应的错误消息。就我个人而言,我更喜欢这种方法:
if (scanner.hasNextInt()) {
a = scanner.nextInt();
} else {
// Can't read the input as int.
// Read it rather as String, and display the error message
String str = scanner.nextLine();
System.out.println(String.format("Invalid input: %s cannot be converted to an int.", str));
}
当然,实现这一点的另一种方法是使用try-catch
block。方法无法将给定的输入转换为整数时抛出InputMismatchException
。因此,您只需要处理输入不匹配异常
:-
try {
int a = scan.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid argument for an int");
}
如果下一个标记无法转换为int
,则可以使用返回false
的方法。然后在else
块中,您可以使用将输入读取为字符串,并打印相应的错误消息。就我个人而言,我更喜欢这种方法:
if (scanner.hasNextInt()) {
a = scanner.nextInt();
} else {
// Can't read the input as int.
// Read it rather as String, and display the error message
String str = scanner.nextLine();
System.out.println(String.format("Invalid input: %s cannot be converted to an int.", str));
}
当然,实现这一点的另一种方法是使用try-catch
block。方法无法将给定的输入转换为整数时抛出InputMismatchException
。因此,您只需要处理输入不匹配异常
:-
try {
int a = scan.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid argument for an int");
}
使用异常
。。每当输入的数字超过其存储容量时,就会引发异常
请参阅docs.oracle.com/javase/tutorial/essential/exceptions/使用异常。。每当输入的数字超过其存储容量时,就会引发异常
请参阅docs.oracle.com/javase/tutorial/essential/exceptions/您可以使用hasNextInt()方法来确保有一个整数可以读取 您可以使用hasNextInt()方法来确保有一个整数可以读取 试试这个:
long num=(long)scan.nextLong();
if (num > Integer.MAX_VALUE){
print error.
}
else
int x=(int)num;
或者尝试捕捉:
try{
int number=scan.nextInt()
}
}catch(Exception ex){
print the error
}
试试这个:
long num=(long)scan.nextLong();
if (num > Integer.MAX_VALUE){
print error.
}
else
int x=(int)num;
或者尝试捕捉:
try{
int number=scan.nextInt()
}
}catch(Exception ex){
print the error
}
+1很好的解决方案,除了尝试捕获之外,我们还有什么可以做的吗?谢谢您的快速回复!我决定先尝试.hasnextInt(),但遇到“无法从静态引用非静态方法”上下文错误。@StephenGilardi。。你是如何使用它的?你能出示你的密码吗?@Rohit Jain。。我确实使用了与您完全相同的上下文,但我将“int a”替换为一个变量,我已经初始化为int,“numberIn”。@StephenGilardi。。您的变量numberIn
可能是一个静态变量。如果没有看到您的实际代码,就无法说出更多信息。+1很好的解决方案,除了try catch
,我们还能做些什么吗?谢谢您的快速回复!我决定先尝试.hasnextInt(),但遇到“无法从静态引用非静态方法”上下文错误。@StephenGilardi。。你是如何使用它的?你能出示你的密码吗?@Rohit Jain。。我确实使用了与您完全相同的上下文,但我将“int a”替换为一个变量,我已经初始化为int,“numberIn”。@StephenGilardi。。您的变量numberIn
可能是一个静态变量。如果看不到你的实际代码,你就说不出什么。这是行不通的。scan.nextInt()返回一个int,它永远不会超过MAX\u值这永远不会起作用。scan.nextInt()返回一个int,它永远不会超过MAX\u value。我得到一个编译错误。“无法从静态上下文引用非静态方法nextInt()”@StephenGilardi在此示例中我没有使用nextInt。是ideone的一个工作示例。我猜你主要是在调用它,Scanner不是类中的静态变量,对吗?如果是这样,请将扫描仪移到主屏幕或使其静止。否。这是在它自己的类“Decimal”中。我有一门驾驶课的主要课程。我会调查你在最后一部分所说的话,尽管我从未注意到你没有使用nextInt。这是完美的。这不像其他(潜在的?)解决方案那样有意义(逻辑上)。这也是我第一次被介绍给try/catch。谢谢你的帮助!我得到一个编译错误。“无法从静态上下文引用非静态方法nextInt()”@StephenGilardi在此示例中我没有使用nextInt。是ideone的一个工作示例。我猜你主要是在调用它,Scanner不是类中的静态变量,对吗?如果是这样,请将扫描仪移到主屏幕或使其静止。否。这是在它自己的类“Decimal”中。我有一门驾驶课的主要课程。我会调查你在最后一部分所说的话,尽管我从未注意到你没有使用nextInt。这是完美的。这不像其他(潜在的?)解决方案那样有意义(逻辑上)。这也是我第一次被介绍给try/catch。谢谢你的帮助!