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. 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
Rolling the dice...
int[] tempIndex = new int[hands.getDrawNum()]; boolean 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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.