I have encountered the same error many times, and I don't know why it happened or what caused it. I tried literally changing everything but it either only allows me to type 2 numbers or just gives me back the same error. What does this error even mean? How can I fix this? Any ideas? (I'm new to StackOverFlow) I'm trying to make this random element picker as you can see below:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Hi! Welcome to the random element picker! How many elements are you going to choose from?");
Scanner input= new Scanner(System.in);
int NumberOfElement= input.nextInt();
String[] Elements= new String[NumberOfElement];
System.out.println("type out all the elements! Press enter after each one!");
for(int i=0;i<=NumberOfElement;i++){
Elements[i]=input.nextLine();
}
int number = (int)(Math.random() * (NumberOfElement-1));
System.out.println("I chose " + Elements[number]);
}
}