views:

12

answers:

1

Hi, i'm new in java and have a phase of code like this:

import javax.swing.JOptionPane;
public class test
{
 public static void main(String[] args) {
  String value=JOptionPane.showInputDialog("please input your value");
  if (value== "1"){
   System.out.println("1");
  }else{
   System.out.println("not 1");
   }
  }
}

Question : why every time i put 1,system print "not 1"?

thanks alot

+4  A: 

Try replacing value == "1" with value.equals("1"). Strings in Java are references and there are no operator overloads to help you with equality. Sometimes the strings are interned and == would work, but not usually. You should always use the equals method.

Kirk Woll
It works. Thanks for your help. :-)
lorne