Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two textbox one is named adminname and other is adminpassword. I want to get input from these two text box and use in if condition but i am not getting the result
here is my Code
Java
String name=adminname.getText();
        String password=adminpassword.getText();
        if(name="anoop" && password="sharma")
        {
            JOptionPane.showMessageDialog(rootPane, "Login successfull");
        }

Please Help me in finding my mistake/error.
Thanx in advance
Posted
Updated 25-Jul-17 0:26am
Comments
[no name] 26-May-13 8:05am    
You need to use equals() to test strings for equality in java.
Anoop Kr Sharma 26-May-13 8:09am    
@ThePhantomUpvoter can you help me in creating the code using equals()
[no name] 26-May-13 8:15am    
You are kidding right? You are seriously unable to to write if(name.equals("anoop")?
Anoop Kr Sharma 26-May-13 8:22am    
Thanx ThePhantomUpvoter for solving my problem

if(new String("test").equals("test")) 
{
  // Expression is true
}

if(new String("test") == "test")
{
  // Expresson is false since you are not comparing 
  // the same instance of the object to itself
}


Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jun-13 23:08pm    
Sure, a 5.
—SA
Espen Harlinn 6-Jun-13 10:21am    
Thank you, Sergey :-D
In Java, '=' is not a comparison operator but assignment. For comparison, you need to replace it with '=='.

—SA
 
Share this answer
 
For assiging we use single = sign, but for comparing we use double = sign.
C#
String name=adminname.getText();
        String password=adminpassword.getText();
        if(name=="anoop" && password=="sharma")
        {
            JOptionPane.showMessageDialog(rootPane, "Login successfull");
        }

Ref :
http://www.pointbaba.com/faq/65/username-and-password-checking-in-java[^]
 
Share this answer
 
Comments
Richard MacCutchan 12-Sep-15 4:03am    
This question is more that two years old and has already been answered. Also your "solution" is incorrect, which you could have discovered by reading the others.

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