import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Example {
public static void main(String[] args) {
String name;
name = JOptionPane.showInputDialog(null, "ENTER TEST BELOW:"," ",3 );
if (name == "TEST") {
JOptionPane.showMessageDialog(null, "Welcome " + name + ", It works "," ", 1, new ImageIcon("Pictures/Example.jpg"));
}
else {
JOptionPane.showMessageDialog(null,"Welcome " + name + ", It doesn't work."," ", 1, new ImageIcon("Pictures/Example.jpg"));
}
}
}
Asked
Active
Viewed 58 times
-3
-
1so what is the error? – H-Patel Aug 29 '13 at 00:04
-
1Don't compare `String` values with `==`; use `String`'s `equals` method instead. – rgettman Aug 29 '13 at 00:06
1 Answers
2
Instead of comparing the strings with this:
name == "TEST"
you should use this:
name.equals("TEST")
A famous question explains the reason why you need to do this. Essentially, ==
compares whether name
and "TEST"
are the same object. They are not the same object, but they have the same contents, and equals
tests for the same contents.