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.
In simple terms
int
is a primitive type for representing a 32-bit integer number. String
is a reference type wrapped arround an array of char
s to represent a sequence of characters.
You cannot just change the type from int
to String
and make the character sequence in the string magically becomes a number.
Think about how you would implement this conversion. You would look at each character, make sure it is a valid digit etc.
Change:
if((int)userNumbers.get(v) > 49)
To:
if(Integer.parseInt(userNumbers.get(v)) > 49)
Further, when creating Generic type containers like ArrayList
make sure you also declare what type of objects they store for type safety reasons.
Change:
ArrayList userNumbers = new ArrayList(Arrays.asList(userNumbersArray));
To:
ArrayList<String> userNumbers = new ArrayList<String>(Arrays.asList(userNumbersArray));
This way you calling userNumber.get()
will return a String
. If you don't put this it will return an Object
so you would actually call Integer.parseInt(Object)
which is invalid. You would get a pretty self-explanatory error looking like this:
The method parseInt(String) in the type Integer is not applicable for the arguments (Object)
EDIT:
For your IndexOutOfBounds
error, you are trying to access more elements than you have. Basically, you want to have 6 values but your for
loop says from 0 until 6 (included)
. That is a total of 7 elements: 0, 1, 2, 3, 4, 5, 6
You can fix this by changing the <= 6
to < 6
in your loop but the best way to fix it so it will work no matter how many elements you have is to use the size()
property of ArrayList
:
Change:
for(int v = 0; v <= 6; v++)
To:
for(int v = 0; v < userNumbers.size(); v++)
First, you should use generics for ArrayList
. Please replace
ArrayList userNumbers = new ArrayList(Arrays.asList(userNumbersArray));
with
ArrayList<String> userNumbers = new ArrayList<>(Arrays.asList(userNumbersArray));
However, this is not your problem. The misconception here is that casting will infer what type of conversion you need; it won't . Replace (int)userNumbers.get(v)
with Integer.parseInt(userNumbers.get(v))
to parse the int
from a String
.
Type casting is not meant to parse an Integervalue from an String.
You should use Integer.parseInt for that.
if(Integer.parseInt(userNumbers.get(v)) > 49
should solve your error.
First, you will get an ArrayIndexOutOfBounds exception because of the <= 6 in your for loop.
Secondly, you should use the Integer.parseInt(String intStr) method to turn each number into its int equivalent.
Here is a stripped down version that I think works:
public static void main(String[] args)
{
String testInputOne = "14,22,18,2,5,11";
boolean allNumbersAreValid = true;
String[] userNumbersStrArray = testInputOne.split(",");
for (String numberStr: userNumbersStrArray)
{
Integer number = Integer.parseInt(numberStr.trim());
if (number > 49)
{
allNumbersAreValid = false;
break;
}
}
System.out.println("Testing " + testInputOne + ": " + (allNumbersAreValid?"All valid!":"At least one invalid"));
}
If you want to further guard against bad input such as someone entering an alpha character, you could wrap the Integer.parseInt call in a try catch block and catch the NumberFormatException and output a suitable error message.
I'm frankly a little scared as to what the recursive call to main is supposed to do? :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.