0

String []lucky = new String[20];

int []Lucky = new int[lucky.length];

    for(int i = 0 ; i < 20 ; i++){
        lucky[i] = JOptionPane.showInputDialog("Enter " +(i+1) +" number : ");
        Lucky[i] = Integer.parseInt(lucky[i]);
        if(lucky[i] == " "){
            Lucky[i] = 0;
        }
        System.out.println("The "+(i+1) +" number user input : "+Lucky[i]);  
    }
moon
  • 3
  • 2

1 Answers1

0

First of all the way you check for an empty string is not correct. Instead of doing == " " you should use String#isEmpty().

Secondly, you try to parse an empty string to a Integer, this will fail with a NumberFormatException (hence why you see you program stop).

So, instead of trying to parse the string to an integer and then check whether the give string is empty, you should first check whether it's empty or not and then parse it to an Integer object.

Ideally you should modify your code to something like this:

for (int i = 0; i < 20; i++) {
    lucky[i] = JOptionPane.showInputDialog("Enter " + (i + 1) + "number: ");
    if (lucky[i].isEmpty()) {
        luckyInt[i] = 0;
    } else {
        luckyInt[i] = Integer.parseInt(lucky[i]);
    }
    System.out.println("The "+(i+1) +" number user input : "+ luckyInt[i]);
}
akortex
  • 5,067
  • 2
  • 25
  • 57