views:

72

answers:

1

Well, I'm trying to create a way of detecting if the user inputs nothing and clicks 'OK.'

For example, if the user clicks cancel, I break out of a while loop with the following code:

if (words[i] == null) break; //breaks out of while loop

I tried something along these lines for a user clicking OK:

else if (Character.isDigit(words[i].charAt(0)) && words[i].charAt(0) == JOptionPane.OK_OPTION) break;

to break out of the loop if the user were to click OK, but no dice. I get this exception:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at Project1.main(Project1.java:21)

OK_OPTION is of type int which is why I converted to int. Does anybody have any ideas of how I can do this?

I found this post about the 'Cancel' button but nothing about the 'OK' button. Thanks!

+1  A: 

When a user enters no input and hits okay, your program is still storing the string in your words array, the string just happens to be blank. An easy way to check if the string is blank is by checking its length, so the code you want could look like:

if( words[i].length() == 0 ) break; //break if user enters blank input

Mark