Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a program, which requires the person to Login first!
My code is as follows:
Java
import comp102x.IO;
/*import java.awt.Toolkit;
import java.lang.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;*/
public class Main
{
    public void loginCheck()
    {
        String correctPassword = new String();
        correctPassword = "sanvar55";
        String password = new String();
        System.out.print("Enter Password:");
        password = IO.inputString();
        System.out.println(password);
        if(password=="sanvar55")
        {
            System.out.println("Correct Password");
            //CopyStringToClipboard(password);
        }
        else
        {
            System.out.println("Incorrect Password!");
        }
    }
    /*private static void CopyStringToClipboard(String input) 
    {
        String str = input;
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Clipboard clipboard = toolkit.getSystemClipboard();
        StringSelection strSel = new StringSelection(str);
        clipboard.setContents(strSel, null);
    }*/
}


What I have tried:

But on executing, I get the following output:
Enter Password:sanvar55
sanvar55
Incorrect Password!

I am not able to understand, why it is doing so?
Posted
Updated 4-Apr-17 7:59am
Comments
[no name] 4-Apr-17 12:52pm    
Because you are comparing string references which are not the same. use equals() to compare your strings.
Arnav Varshney 5-Apr-17 1:18am    
Thanks!

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900