0

My Java program isn't working! Here is my code:

import javax.swing.JOptionPane;
public class practice
{
public static void main(String[] args)
{
    String userName = "Eddie"; //username is Eddie
    String passWord = "hI";    // passsword is hI
    String name;
   name = JOptionPane.showInputDialog("Whats your username? "); 

   if (name == userName)
   JOptionPane.showMessageDialog(null, "Exepted!");
   System.exit(0);


}
}

Its supposed to ask for my username, and if I type "Eddie" it has to show message dialog "Accepted".But after I type "Eddie" the program closes instead! Any suggestions?

Naeem Ul Wahhab
  • 2,465
  • 4
  • 32
  • 59
Eddie
  • 63
  • 2
  • 2
  • 9

3 Answers3

0

if (name == userName) should be changed to if (name.equals(userName))

Both equals() and "==" operator in Java is used to compare objects to check equality but main difference between equals method and == operator is that former is method and later is operator.

Read more here to identify the differences: http://javarevisited.blogspot.com/2012/12/difference-between-equals-method-and-equality-operator-java.html#ixzz44dFcXkwr

Akash Rajbanshi
  • 1,553
  • 11
  • 23
  • Thank you. can you explain the difference between "==" and equals() – Eddie Apr 02 '16 at 02:35
  • 1
    if you use `==` to check the equality of string it will check whether they are the same object or not i.e. they have the same reference. But if you use `equals()`, it compares the value of the required strings. – Akash Rajbanshi Apr 02 '16 at 02:38
0

Needs to be if (name.equals(userName)) also, I believe your word "exepted" needs to be accepted

user2277872
  • 2,963
  • 1
  • 21
  • 22
0

It should be noted that == compares whether two references point to the same object, while the equals() method (for String) checks whether two objects have the same value. What do I mean by this? What's really the difference?

When you use == to compare Strings, it's checking to see if the two Strings you're comparing point to the exact same location in memory. Note that I could have String a = "abc" and String b = "abc", but a and b don't reference the same memory (they might represent two different memory locations, and those memory locations both happen to hold the same value, "abc"). This is exactly what is happening in your example, except the value of your Strings is "Eddie". equals(), on the other hand, compares any two Strings to see if they have the same characters.

Crummy analogy:

Image the JVM is a waiter. Say you see your friend Bob with a bacon cheeseburger with crispy onions, and it looks appetizing. Further assume you want to ask the JVM if you can have what Bob is having. If you use ==, the JVM will rip the burger Bob is eating right out from under his nose, and hand you that burger. That exact burger. If you use equals(), the JVM will get you another bacon cheeseburger with crispy onions from the kitchen. You almost always want equals() with String comparisons. Don't steal Bob's burger!!!

Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52