Need help with my program
hi, is there any way to try-catch an input mismatch exception in a do-while loop? here is a sample of the code:
int menu;
do{
menu = sc.nextInt();
switch(menu)
{
case 1:
switch(option)
{
case 1:
...
}
case 2:
...
default:
System.out.println("Please enter from option 1-2");
}
}while(menu != 2 );
whenever a user enter alphab the program will crash and if i add a try-catch inside the do-while loop it will loop infinitely...
Yes, you can use try-catch in the loop. What did you try? Kaj
> hi, is there any way to try-catch an input mismatch> exception in a do-while loop? Yes.Ted.
As for the code you posted, I think the sc.nextInt() method is the one in charge of getting the input, I would suggest one of two things:
Either nextInt() method handles the exception inside and return an specific integer code determining a match error, or just add a try-catch as you said (maybe the problem is where you place it)
A:
//Suposing sc's class is SC and it has a variable error
//public static final int ALPHA_CHAR = -255;
int menu;
do{
menu = sc.nextInt();//suposing nextInt() returns -255 if alpha
switch(menu){
case 1:
switch(option){
case 1:
...
}
case 2:
...
case SC.ALPHA_CHAR:
System.out.println("Only numbers accepted, choose from option 1-2");
default:
System.out.println("Please enter from option 1-2");
}
}while(menu != 2 );
B:
int menu;
do{
try{
menu = sc.nextInt();
switch(menu){
case 1:
switch(option){
case 1:
...
}
case 2:
...
default:
System.out.println("Please enter from option 1-2");
}
}catch(Exception e){
System.out.println("Please enter from option 1-2");
}
}while(menu != 2 );
Hope this helps, cya around.
this i where i add the try-catch actually, but it will make the do-while loop infintely non-stop....
int menu;
do{
try{
menu = sc.nextInt();
switch(menu)
{
case 1:
switch(option)
{
case 1:
...
}
case 2:
...
default:
System.out.println("Please enter from option 1-2");
}catch(InputMismatchException e){System.out.println("Please enter numbers from 1-2 only");}
}while(menu != 2 );
import java.io.*;
public class KeyboardRead {
public static void main(String args[]) throws IOException {
String opt;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Enter ur option(1/2) : ");
opt = br.readLine();
if(!opt.equals("1") && !opt.equals("2")) System.out.println("Error: Pls enter a valid option!");
}
while(!opt.equals("1") && !opt.equals("2"));
switch(Integer.parseInt(opt)) {
case 1: System.out.println("Operations for case-1"); break;
case 2: System.out.println("Operations for case-2"); break;
}
}
}
Cheers
I guess sc is a java.util.Scanner
When you encounter and try to deal with an InputMismatchException the
scanner is not advanced past the input that was seen. (See the API
documentation). Hence when you have some alpha input you get the
infinite loop because the bad characters are seen, the exception is
thrown, then the same characters are seen again, the exception is
thrown again, etc.
Perhaps you should include something like sc.next() in the catch block
to read (and discard) the bad characters.
thanks for all the help..just need to add sc.nextLine() and everything solved :)