Results 1 to 3 of 3
- 07-21-2012, 05:34 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Password Program Not Working Correctly
So I just started learning Java, and I'm writing a program that is supposed to ask the user for a password, then checks it with a stored variable that has the password, and if correct it states "Correct", if incorrect it states "Incorrect password, please try again" and if left blank it states "Please enter a password". I have fixed the obvious bugs, and it compiled. But the bug I'm having is it always says the password is incorrect. I'm not sure exactly what's wrong, but I think the user input isn't being stored correctly. Any help is greatly appreciated. Also, sorry if my spacing is crappy, suggestions welcome. Here's the code:
Java Code:import java.util.Scanner; public class Password { public static void main (String [] args) { Scanner pass_input=new Scanner(System.in); String pass; System.out.println("Enter Password:"); pass = pass_input.next(); if (pass=="lulz") { System.out.println("Correct"); } else if (pass=="") { System.out.println("Please enter a password."); } else { System.out.println("Incorrect password. Please try again"); } } }
- 07-21-2012, 05:57 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Password Program Not Working Correctly
Never compare two Strings for equality with the == operator; use the String.equals( ... ) method instead.
kind regards,
Jos
ps. I think I typed this a thousand times now, so I deserve a reward ...When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-21-2012, 06:12 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Re: Password Program Not Working Correctly
Thanks Jos! You want a reward, well here it is:http://tinyurl.com/co9t797
Similar Threads
-
Help with If/Else not working correctly?
By kalaiss in forum New To JavaReplies: 8Last Post: 04-16-2012, 06:32 AM -
method not working correctly
By r1b in forum New To JavaReplies: 4Last Post: 01-04-2012, 08:09 PM -
Trying to use the Paint but program isn't working correctly
By quickfingers in forum New To JavaReplies: 2Last Post: 12-28-2011, 09:15 AM -
Applet not working correctly?
By Beavotropper2 in forum Java AppletsReplies: 2Last Post: 04-18-2011, 06:32 AM -
[SOLVED] \t not working correctly?
By Gakusei in forum New To JavaReplies: 5Last Post: 05-06-2008, 04:45 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks