java - Program that should work, but it isn't -
this question has answer here:
- how compare strings in java? 23 answers
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 + ", works "," ", 1, new imageicon("pictures/example.jpg")); } else { joptionpane.showmessagedialog(null,"welcome " + name + ", doesn't work."," ", 1, new imageicon("pictures/example.jpg")); } } }
instead of comparing strings this:
name == "test"
you should use this:
name.equals("test")
a famous question explains reason why need this. essentially, ==
compares whether name
, "test"
same object. not same object, have same contents, , equals
tests same contents.
Comments
Post a Comment