我正在尝试处理异常,但是我收到了其他消息。我不知道为什么收到这个。
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at P4.main(P4.java:28)
我想处理ArrayOutOfBoundsException。 谢谢!
public class P4 {
public static void main(String args[]) {
Scanner keyboard=new Scanner(System.in);
ArrayList <Double> sir = new ArrayList<Double>();
System.out.println("Please insert doubles: ");
do {
double x=keyboard.nextDouble();
sir.add(x);
}while(keyboard.hasNextDouble());
System.out.println("Elements in the array are: " + sir);
System.out.println("Enter the index of the required element: ");
try {
int element = keyboard.nextInt();
System.out.println("Element in the given index is :: "+sir.get(element));
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("The index you have entered is invalid");
}
}
}
答案 0 :(得分:-1)
我从catch(ArrayIndexOutOfBoundsException e)
修改为
catch(IndexOutOfBoundsException e)
。这就是答案。