Yes.
Here is some sample output from the program.
Different catch{} blocks
catch the two different exceptions.
The InputMismatchException
did not contain a message.
C:\chap81>java IndexPractice
Enter the data:8
Enter the array index:10
This is your problem: 10
Here is where it happened:
java.lang.ArrayIndexOutOfBoundsException: 10
at IndexPractice.main(IndexPractice.java:18)
Good-by
C:\chap81>java IndexPractice
Enter the data:rats
This is your problem: null
Here is where it happened:
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at IndexPractice.main(IndexPractice.java:15)
Good-by
A more sensible program would use a loop to collect data from the user and place it in the array. Exceptions would cause the program to repeat the prompt. This would be much more user-friendly than halting. If a user had already entered 999 data items, it would not be friendly to crash if item 1000 is wrong!
What does the "15" mean in
at IndexPractice.main(IndexPractice.java:15)