I'm trying to keep up with you youngsters, so I'm taking a beginning Java course. As part of a homework assignment, we were to prompt the user for a number between 1 & 3. We also used while loops in the chapter, so I decided to put the prompt in a while loop to force the user to enter 1, 2 or 3. I was very surprised when it did not work, so I have written a 5-liner to show you the problem:
[code=java]
import javax.swing.JOp tionPane;
public class Weird {
public static void main(String[] args)
{
String s = "";
while (s != "1" && s!= "2" && s!= "3")
{
s = JOptionPane.sho wInputDialog(nu ll, "Enter 1, 2 or 3");
if (s == null) break;
}
if (s!= null) JOptionPane.sho wMessageDialog( null, s);
}
}
[/code]
I fixed the problem by parsing the string and testing for integers 1, 2, or 3, but the above code should work! The same code in VB works great:
[code=vb]
Sub NotWeird()
Dim s As String
Do While (s <> "1" And s <> "2" And s <> "3")
s = InputBox("Enter 1, 2 or 3")
Loop
MsgBox s
End Sub
[/code]
Looking at the Java code in the debugger, after the user enters 1, the debugger says s = "1" So far, great, but if I inspect s != "1", it says "s != "1"" = true
What is this crazyness?!?
[code=java]
import javax.swing.JOp tionPane;
public class Weird {
public static void main(String[] args)
{
String s = "";
while (s != "1" && s!= "2" && s!= "3")
{
s = JOptionPane.sho wInputDialog(nu ll, "Enter 1, 2 or 3");
if (s == null) break;
}
if (s!= null) JOptionPane.sho wMessageDialog( null, s);
}
}
[/code]
I fixed the problem by parsing the string and testing for integers 1, 2, or 3, but the above code should work! The same code in VB works great:
[code=vb]
Sub NotWeird()
Dim s As String
Do While (s <> "1" And s <> "2" And s <> "3")
s = InputBox("Enter 1, 2 or 3")
Loop
MsgBox s
End Sub
[/code]
Looking at the Java code in the debugger, after the user enters 1, the debugger says s = "1" So far, great, but if I inspect s != "1", it says "s != "1"" = true
What is this crazyness?!?
Comment