-1

I am trying to make a simple password protected program. The idea is that when you type in the correct password, an "Access Granted" message will appear. If the password is typed incorrectly, the message, "Access Denied", appears. This is run on the If/Else Statements. The problem with my program is that even when I type the correct password into the console, the else statement is still runs.

Despite having no errors, (Besides a Resource Leak) this happens. Here is my code:

import java.util.Scanner;

public class PasswordProtected {
    public static void main (String args[]){
        Scanner Password = new Scanner (System.in);
        String mainpassword, userInput;
        mainpassword = ("bob");
        System.out.println("Please enter the password to continue.");
        userInput = Password.nextLine();
        System.out.println("Verifying Password");
        if (userInput == mainpassword){
            System.out.println("Access Granted");
        }else{
            System.out.println("Access Denied");
        }
    }

}

This is what my console produces when the password is correct:

Please enter the password to continue.
bob
Verifying Password
Access Denied

This is what my console produces when my password is wrong:

Please enter the password to continue.
erdtfyhujnikyguj
Verifying Password
Access Denied

Could anyone explain to me why this is happening? Is my code wrong? Could someone perhaps help me fix it?

Jake
  • 153
  • 1
  • 7
  • As for the Duplicate mark, the question that you would have sent me to would not have been useful as I did not know of ".equals". – Jake Apr 22 '15 at 02:45
  • No worries, closing the question as a duplicate isn't indicting you for not doing research. The point is, your question *does* boil down to that question, and so it's both an answer to your question, and more useful for future visitors. It's not a personal thing, just a housekeeping thing. – Mark Peters Apr 22 '15 at 02:48
  • 1
    @Jake, I'm not sure you've thought that through. Since you _clearly_ have a problem in comparing strings in Java, I would think a question entitled "How do I compare strings in Java?" would be a pretty safe bet for something to look at :-) In any case, closure as duplicate is not a negative indication of your question's _value,_ just that it's been asked and answered before. – paxdiablo Apr 22 '15 at 02:48

3 Answers3

5

You should be using the equality function (.equals) to compare objects and not the == operator.

Strings are objects so when you use the == operator, you are checking references not contents.

Zian Choy
  • 2,846
  • 6
  • 33
  • 64
1

When comparing reference types like strings in Java use the .equals() method rather than ==. The latter will compare object identity rather than values.

if (userinput.equals(mainpassword))
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Travis Smith
  • 542
  • 1
  • 6
  • 21
0

you are using == operator to compare strings which is not correct.

try compareTo string method

if (userInput.compareTo(mainpassword) == 0){
 System.out.println("Access Granted");
} else {
System.out.println("Access Denied");
}

-1 or 1 is returned if string1 > string 2 and vice-versa. If they are the same, zero (0) is returned. However it will cause problems if you have trailing spaces or mismatch of case.

Better alternative

string equals method

if (userInput.equals(mainpassword)){
 System.out.println("Access Granted");
} else {
System.out.println("Access Denied");
}

when the two strings contain the exact same string of characters, the equals method will return true

Ashesh
  • 3,499
  • 1
  • 27
  • 44