Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Password Program Not Working Correctly

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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");
    	}
     
      }
        
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default 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 ...
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Password Program Not Working Correctly

    Thanks Jos! You want a reward, well here it is:http://tinyurl.com/co9t797

Similar Threads

  1. Help with If/Else not working correctly?
    By kalaiss in forum New To Java
    Replies: 8
    Last Post: 04-16-2012, 07:32 AM
  2. method not working correctly
    By r1b in forum New To Java
    Replies: 4
    Last Post: 01-04-2012, 09:09 PM
  3. Replies: 2
    Last Post: 12-28-2011, 10:15 AM
  4. Applet not working correctly?
    By Beavotropper2 in forum Java Applets
    Replies: 2
    Last Post: 04-18-2011, 07:32 AM
  5. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 05:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •