Error retrieving Int from Java String


I keep getting an "invalid int" error in logcat, how do I correct this?

check.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           String[] guessArray = digits.split("");
           int[] guessInt = new int[6];
               guessInt[i] = Integer.parseInt(guessArray[i]);
               if(guessInt[i] == actualInt[i]){
                   easyGuessDisplay.setText("Sorry! Try again!");

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(
            at java.lang.Integer.parseInt(
            at java.lang.Integer.parseInt(

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]);