[英]NumberFormatException printing before necessary error
I'm getting the following results when I run the following section of my code it works well it just appears to print the NumberFormatException before running the do-while loop even though that isn't an option in my code at least not intended.当我运行我的代码的以下部分时,我得到以下结果它运行良好它只是在运行 do-while 循环之前打印 NumberFormatException 即使这不是我的代码中的一个选项,至少不是故意的。 Please help me to understand this error thank you!
请帮我理解这个错误谢谢!
Console output: Enter the number of dice to draw (re-roll) - up to 3: 2 Enter the index numbers (0 to 3) separated by a space of the dice you wish to draw: java.lang.NumberFormatException: For input string: "" Try again Enter the index numbers (0 to 3) separated by a space of the dice you wish to draw: 0 1控制台输出:输入要绘制的骰子数(重新滚动) - 最多 3:2 输入索引号(0 到 3),由您要绘制的骰子的空格分隔:java.lang.NumberFormatException:对于输入字符串:"" 再试一次 输入索引号(0 到 3),用空格隔开你想抽的骰子:0 1
Rolling the dice...掷骰子...
int[] tempIndex = new int[hands.getDrawNum()]; int[] tempIndex = new int[hands.getDrawNum()]; boolean tryAgain = true;
布尔 tryAgain = true;
do {
try {
System.out.print("Enter the index numbers (0 to 3) separated by a space "
+ "of the dice you wish to draw: ");
String templine = input.nextLine();
String[] templineSplit = templine.split(" ");
for (int i = 0; i < hands.getDrawNum(); i++) {
tempIndex[i] = (Integer.parseInt(templineSplit[i]));
}
tryAgain = false;
for (int i = 0; i < hands.getDrawNum(); i++) {
if(!((tempIndex[i] >= 0) && (tempIndex[i] <= 3))) {
tryAgain = true;
throw new IllegalArgumentException();
}
}
}
catch (InputMismatchException ime) {
System.out.println(ime);
System.out.println("Try again");
tryAgain = false;
}
catch (IllegalArgumentException iae) {
System.out.println(iae);
System.out.println("Try again");
}
catch (ArrayIndexOutOfBoundsException aiob) {
System.out.println(aiob);
System.out.println("Try again");
tryAgain = false;
}
} while(tryAgain);
The NumberFormatException
occurs when an attempt is made to convert a string with improper format into a numeric value尝试将格式不正确的字符串转换为数值时会发生
NumberFormatException
The only place where this can happen in your code is here在您的代码中唯一可能发生这种情况的地方是这里
tempIndex[i] = (Integer.parseInt(templineSplit[i]));
Use a debugger or print out all values in tempIndex
to a console to see which one is not a valid number.使用调试器或将
tempIndex
中的所有值打印到控制台以查看哪个不是有效数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.