I am very new at Java. I am trying to write a lottery simulator. I have the following code:
Scanner input = new Scanner(System.in);
System.out.println("Enter six whole numbers between 1 and 49 in a comma-separated list: ");
String userNumbersString = input.nextLine();
String[] userNumbersArray = userNumbersString.replaceAll("\\s+","").split(",");
ArrayList userNumbers = new ArrayList(Arrays.asList(userNumbersArray));
boolean validNumbers = true;
for(int v = 0; v <= 6; v++){
if((int)userNumbers.get(v) > 49){
validNumbers = false;
v = 7;
}else{
v++;
}
}
if(validNumbers == false){
String[] str = new String[0];
main(str);
}
It compiles properly. However, when I input a series of numbers, I get this error:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer.
I am very confused about why this string can't be cast to an integer.
Sorry if this is a long-winded question, but I would hugely appreciate any help.
I have just realised that the OutOfBounds exception was due to a stupid maths mistake on my part; v was starting at 0; there were five values in the array, but I gave the for loop the condition <= 6. Sorry about that.
Thank you everybody for all your help. Together, you have resolved my question.