Exception handling leading to infite loop (java)
Hello I am a Java beginner. I need a method that accepts an integer from 1 to 9. I am using an exception handler so that it can deal with incorrect or inconsistent input, but it seems like it only executes the "choice = input.nextInt ()" statement once, and my loop becomes infinite because of it.
Code below:
import java.util.*;
public class Player{
private int[] over;
private int choice;
private int coordinates[];
private static Scanner input = new Scanner(System.in);
public Player(){
over = new int[5];
for(int i = 0; i < 5; i++){
over[i] = 1;
}
coordinates = new int[2];
coordinates[0] = coordinates[1] = -1;
}
public void getChoice(){
int choice = -1;
boolean inputIsOk;
do{
System.out.print("Enter Your Choice: ");
inputIsOk = true;
try{
choice = input.nextInt();
}
catch(InputMismatchException e){
System.out.println("Invalid choice");
inputIsOk = false;
}
if(choice < 1 || choice > 9){
System.out.println("Enter Choice In Range(1-9)");
inputIsOk = false;
}
}while(!inputIsOk);
System.out.println("You Entered "+choice);
}
}
And here is the test class:
public class TestPlayer{
public static void main(String args[]){
Player p1 = new Player();
p1.getChoice();
}
}
Here is the conclusion: First case When only integral choice is introduced
harsh@harsh-Inspiron-3558:~/java/oxgame$ javac TestPlayer.java
harsh@harsh-Inspiron-3558:~/java/oxgame$ java TestPlayer
Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: -1
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice: 5
You Entered 5
And secondly, when I entered the wrong input:
Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice:g
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
and it goes on....
Please help me, thanks.
source to share
This will work
try{
choice = Integer.parseInt(input.next());
}
catch(NumberFormatException e){
System.out.println("Invalid choice");
inputIsOk = false;
}
Reason Genesis: Sky Scanner reads an object from a stream, say typeCache
. Until it receives an Integer value, the buffer will not be flushed, but typeCache
will hold String
until it is read by next()
(or any equivalent).
Code Scanner
class:
public int nextInt() {
return nextInt(defaultRadix);
}
public int nextInt(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Integer)
&& this.radix == radix) {
int val = ((Integer)typeCache).intValue();
useTypeCache();
return val;
}.......
Or Just add input.next();
to your catch block and it will automatically clean up typeCache
.
source to share
if you change the catch clause like below:
} catch (InputMismatchException e) {
input.next();
System.out.println("Invalid choice");
inputIsOk = false;
}
this will work, input.next();
I don't know exactly why, the old code - when you entered g - just executed this choice = input.nextInt();
as if it was still storing the same value, it didn't wait for user input, calling next()
fixed it.
source to share