Enter 5 integers from the command line, put in an integer array, and print the output. Requirements:
If the input data is not an integer, to catch an input mismatch exception, display "Please enter an integer", or if the input data is 5 extra, capture the array out of bounds exception, display "Please enter 5 integers".
Whether or not an exception occurs, output "Thanks for using this program!" ”
Importjava.util.InputMismatchException; ImportJava.util.Scanner; Public classTest { Public Static int[] number; Public Static voidMain (string[] args) { number=New int[5]; Scanner input=NewScanner (system.in); Try { for(inti=0;i<6;i++) { if(i==5) Throw NewException ("Please enter 5 integers. "); Number[i]=Input.nextint (); } } Catch(inputmismatchexception e) {System.err.println ("Please enter integer data. "); }Catch(Exception e) {e.getmessage (); E.printstacktrace (); }finally{input.close (); System.out.println ("Thank you for using this program. "); } } }
Operation Result:
Java Input Data exception handling