Java Notes
Dialog Box Input Loop
Indicating end of input with cancel or close box, a special value, or empty input
When reading input in a loop user must have some way of indicating that the end of the input has been reached.
sentinel value. One way to indicate the end of the input is for the user to enter a special value to indicate that the end has been reached. This generally works, but does eliminate a possible value from the input. This may not be a problem in many cases, but it can be. A common special value is just the empty string.
Special signal - null. A better way to indicate the end of the input is
often to do something special. For dialog box input, this could be clicking
the close box or hitting the Cancel button. This is easy to test because
a null value is returned in those cases. You could have code like
while (true) { // Infinite loop must contain a break intStr = JOptionPane.showInputDialog(null, "Enter an int or CANCEL.")); if (inStr == null) { break; // Exit the current loop } int n = Integer.parseInt(intStr); . . .
Alternate if style may be use for exits. Sometimes an if statement is only used
to exit from a loop (break), a method (return), or
with an exception (throw).
In these cases some programmers will omit the braces and write the exit action
on the same line. For example.
while (true){ // Infinite loop must contain a break
intStr = JOptionPane.showInputDialog(null, "Enter an int or CANCEL."));
if (inStr == null) break;
int n = Integer.parseInt(intStr);
. . .
Adding test for empty string. Another common way for the user
to indicate the end of the input is simply to hit enter without typing anything.
This returns the empty string, a valid string, but one with zero
characters in it. Here are two ways to test for the empty string.
Note that you have to use the equals(...) method when
comparing the content of strings.
if (inStr.equals("")) ... // Same as below.
if (inStr.length() == 0) ... // Same as above.
Here is the same code as in the previous example, with a test for empty input.
while (true){ // Infinite loop must contain a break
intStr = JOptionPane.showInputDialog(null, "Enter an int or CANCEL."));
if (inStr == null) break; // Exit from loop on cancel.
if (inStr.equals("")) break; // Exit from loop on empty input.
int n = Integer.parseInt(intStr);
. . .
Order matters. The order of the previous tests is important.
The null value isn't a string, but a special value that means
there is no string. It's the value that's used for all object types to
indicate that there is no associated object for that variable.
The equals(...) and length() methods both require
a string, not a null, to work.
Combining input and testing in one expression.
When you're just testing for null, as is common when reading from a text file,
it's easier to combine the call and test in the while condition.
This tends to be much less readable and has the somewhat unusual embedded assignment
in it. Don't try using this unless you feel comfortable with it.
For example.
while ((intStr = JOptionPane.showInputDialog(null, "Enter an int or CANCEL.")) != null) {
. . .