• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java question!

Zeeky Boogy Doog

Platinum Member
package mypackage1;
import javax.swing.*;
public class Password2
{
public static void main(String[] args) throws Exception
{

String password;

password = JOptionPane.showInputDialog("Enter Password");
System.out.println("The password is " + password);
if (password == "password")
{
System.out.println("You Win");
}
else
{
System.out.println("You Lose");
}
System.exit(0);
}
}

i'm in my first java class, and i'm having trouble with the problem, for some reason whenever i enter even the correct password, it goes to the else line, rather than the first if statement it should go to, i also tried assigning "password" to a variable and having if (password == input) where input = "password"
the teacher can't figgure it out either...
 
I think the reason that doesn't work is that the object named 'password' is being compared to the literal string "password". What it is really doing is comparing objects, not their string values. And while their string values match, the object representing the string "password" is not the same as the object named 'password'.

What the other folks suggested should work... you want to compare the object's contents, not the internal java object identifier.

-Matt
 
As far as an explanation, you cannot use == for string comparision. You need to use java's built in methods like .equals, as I mentioned earlier.
 
You need to learn about == vs .equals.

== is comparing for reference equality. As in, are two references pointing to the same object in the heap. So, .equals is the method the class writer wrote so that he determines under what conditions two objects are "equal". You can write this method in your own classes.
 
Originally posted by: CorporateRecreation
As far as an explanation, you cannot use == for string comparision. You need to use java's built in methods like .equals, as I mentioned earlier.

:thumbsup:
 
the .equals() thing worked, thanks, and i agree about the teacher, i've had to pick everything up on my own so far, and the .equals hasn't been covered in the book yet, its rather annoying, she "remembered it" after i showed her the working program... yeah... thanks everyone
 
Originally posted by: ArmchairAthlete
You need to learn about == vs .equals.

== is comparing for reference equality. As in, are two references pointing to the same object in the heap. So, .equals is the method the class writer wrote so that he determines under what conditions two objects are "equal". You can write this method in your own classes.

That's what I was trying to say above, but I think your description is much more elegant. 🙂

:beer:
-Matt

 
You should still do it my way - the subtle difference being that if the user cancels the password dialog you'll receive null as the entered String. All the other ways will throw a NullPointerException in that scenario.
 
I think the reason that doesn't work is that the object named 'password' is being compared to the literal string "password". What it is really doing is comparing objects, not their string values. And while their string values match, the object representing the string "password" is not the same as the object named 'password'.

What the other folks suggested should work... you want to compare the object's contents, not the internal java object identifier.
 
Originally posted by: agnitrate
I think the reason that doesn't work is that the object named 'password' is being compared to the literal string "password". What it is really doing is comparing objects, not their string values. And while their string values match, the object representing the string "password" is not the same as the object named 'password'.

What the other folks suggested should work... you want to compare the object's contents, not the internal java object identifier.

They are both strings. The == here is seeing if they both point to the same string object.

http://java.sun.com/j2se/1.5.0/docs/api...html#showInputDialog(java.lang.Object)

(returns a String).

This would make the == return true:

String a = "ATOT";
String b = a;

if(a == b)
{
System.out.println("a and b point to the same string in memory");
}


EDIT: If you want to REALLY get confused about how Java handles Strings sorta like primitives and sorta like Objects, check this crap out:



String c = new String("blah");
String d = new String("blah");

if(c == d)
{
System.out.println("Won't print as c and d do not point to the same string in memory");
}

// VS this :

String a = "blah";
String b = "blah";

if(a == b)
{
System.out.println("This WILL print. Java just made b point to a instead of making a new
string object!");
}
 
Originally posted by: ArmchairAthlete
Originally posted by: agnitrate
I think the reason that doesn't work is that the object named 'password' is being compared to the literal string "password". What it is really doing is comparing objects, not their string values. And while their string values match, the object representing the string "password" is not the same as the object named 'password'.

What the other folks suggested should work... you want to compare the object's contents, not the internal java object identifier.

They are both strings. The == here is seeing if they both point to the same string object.

http://java.sun.com/j2se/1.5.0/docs/api...html#showInputDialog(java.lang.Object)

(returns a String).

This would make the == return true:

String a = "ATOT";
String b = a;

if(a == b)
{
System.out.println("a and b point to the same string in memory");
}


EDIT: If you want to REALLY get confused about how Java handles Strings sorta like primitives and sorta like Objects, check this crap out:



String c = new String("blah");
String d = new String("blah");

if(c == d)
{
System.out.println("Won't print as c and d do not point to the same string in memory");
}

// VS this :

String a = "blah";
String b = "blah";

if(a == b)
{
System.out.println("This WILL print. Java just made b point to a instead of making a new
string object!");
}


^
|
What he said.

But yeah, Java's an intersting beast.
 
Originally posted by: znaps
You should still do it my way - the subtle difference being that if the user cancels the password dialog you'll receive null as the entered String. All the other ways will throw a NullPointerException in that scenario.
^ Correct answer. I'm surprised no one saw the problem with the other solutions.
 
Back
Top