Hello guys,
A few days ago I posted a similar question about a Java code, here's one more, I hope you can help me out.
The problem I have is this, when you enter a character the program is supposed to loop the whole thing AND run the scanner asking for data until the user (assuming the user is quite dumb) finally enters an integer value, I'm getting an endless loop I don't know why and the program won't run the scanner a second time, why's that?
import java.util.*;
public class tryCatch {
public static void main (String args[]){
Scanner ms = new Scanner(System.in);
int number;
int x=1;
do{
try{
System.out.println("Enter your number: ");
number = ms.nextInt();
System.out.println("Your number is: " + number);
x=2;
}
catch(InputMismatchException f){
System.out.println("Error! integer values, only!");
}
}while(x==1);
}
}
A few days ago I posted a similar question about a Java code, here's one more, I hope you can help me out.
The problem I have is this, when you enter a character the program is supposed to loop the whole thing AND run the scanner asking for data until the user (assuming the user is quite dumb) finally enters an integer value, I'm getting an endless loop I don't know why and the program won't run the scanner a second time, why's that?
import java.util.*;
public class tryCatch {
public static void main (String args[]){
Scanner ms = new Scanner(System.in);
int number;
int x=1;
do{
try{
System.out.println("Enter your number: ");
number = ms.nextInt();
System.out.println("Your number is: " + number);
x=2;
}
catch(InputMismatchException f){
System.out.println("Error! integer values, only!");
}
}while(x==1);
}
}