Click to See Complete Forum and Search --> : What's Wrong With My While Loop?


Heliozentric
09-18-2009, 11:15 AM
For some reason this code runs the while loop no matter what... please help.

Here's the source:

import javax.swing.JOptionPane;

public class lab4
{
public static void main(String[] args)
{
String level;
String problem;
String input;

input = JOptionPane.showInputDialog("Select a level (1-4)");
while(input != "1" || input !="2" || input !="3" || input !="4")
{
input = JOptionPane.showInputDialog("Select a level (1-4)");
}

level = input;
JOptionPane.showMessageDialog(null, input);

}
}

Or you can view/edit the source here: http://pastie.org/621870

Thanks in advanced.

DrYap
09-19-2009, 03:40 AM
Hi Heliozentric,

I'm not sure why this is happening.

This code works fine by parsing the string to an int, but I don't know why the string cannot be tested.
import javax.swing.JOptionPane;

public class test
{
public static void main(String[] args)
{
String level;
String problem;
String input;

do
{
input = JOptionPane.showInputDialog("Select a level (1-4)");
}
while (Integer.parseInt(input) != 1 && Integer.parseInt(input) != 2 && Integer.parseInt(input) != 3 && Integer.parseInt(input) != 4);


level = input;
JOptionPane.showMessageDialog(null, input);

}
}


In your loop you were using the logical or "||" operator and that wasn't going to have the effect you wanted, it should have been the and "&&" operator. But I changed that and it still didn't work. The code I've given works but it's a bit clumsy.

Stephen Philbin
09-19-2009, 08:11 AM
Isn't it because you're using the != operator on a string when you should be using the String.equals() method?

Does replacing while(input != "1" || input !="2" || input !="3" || input !="4")
with while(!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4"))
make it start working like you want it to?

svidgen
10-11-2009, 02:33 PM
If we examine this conditional apart from the rest of your script, we see that the conditional resolves to TRUE regardless of the value of input:
(input != "1" || input !="2" || input !="3" || input !="4")
Why? input must hold either a value of 1 or not 1 (this is a basic logical truth).

If input = <something other than 1>, condition 1 is met.
If input = 1, condition 1 fails, but condition 2 is met.

Since these are only two possible conditions, we can stop here. The first two conditions have shown that the conditional is a tautology (it is always true).

DrYap's solution ought to work.

Though, for this type of conditional, the optimal [scalable] solution involves either converting the input into an integer and performing a simple range check or creating an indexed array (or hashmap [derivative]) that holds valid values and checking the input value against the array (or hashmap).

myrtle1908
10-11-2009, 11:49 PM
You can also use regular expressions to eliminate the need for coding multiple conditions eg.

String input = "2";
System.out.println((input.matches("[1-4]")) ? "In range" : "Not in range");