I keep getting an "invalid int" error in logcat, how do I correct this?
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
digits.replaceAll("[^0-9]","");
String[] guessArray = digits.split("");
int[] guessInt = new int[6];
for(i=0;i<5;i++){
guessInt[i] = Integer.parseInt(guessArray[i]);
if(guessInt[i] == actualInt[i]){
correct++;
}
else{
easyGuessDisplay.setText("Sorry! Try again!");
break;
}
}
}
});
in this case digits
is a String I split into an array and then the value of each index is compared to a previously stored array. I get a crash and logcat yells at me:
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
I want to extract all the integers from digits
and store them in a new array for comparison.
Strings are immutable and hence not modified in place.
You need
digits = digits.replaceAll("[^0-9]","");
Also, split("")
will return leading empty string before Java 8. You need to account for that too.
Probably do,
guessInt[i] = Integer.parseInt(guessArray[i + 1]);