I just know basic of try and catch . so this is toocomplicated for me.. so could
ID: 3619001 • Letter: I
Question
I just know basic of try and catch . so this is toocomplicated for me..so could you help me out with comment..
thank you in advace..
Write a program that prompts the user to enter a series of integersfrom the command line (-1 to stop). Use Java’s Exceptionhandling to ignore invalid input. I just know basic of try and catch . so this is toocomplicated for me..
so could you help me out with comment..
thank you in advace..
Write a program that prompts the user to enter a series of integersfrom the command line (-1 to stop). Use Java’s Exceptionhandling to ignore invalid input.
Explanation / Answer
If you try to enter a float or double value, it will throw anexception. Code: import java.io.BufferedReader; import java.io.InputStreamReader; public class Test { public static void main(String[] args) { while(true) { // prompt the user to enter their name System.out.print("Enter your Integer(-1 to exit): "); BufferedReader br = new BufferedReader(newInputStreamReader(System.in)); int nInt = 0; try { nInt = Integer.parseInt(br.readLine()); if(nInt==-1) break; } catch (Exception exp) { System.out.println("error reading aninteger"); } } } }