-4

I have a problem with my code. This code should check login and password. The problem is the else if statement showing a wrong output. Example, when i enter username == admin and password == 1234, it display username is incorrect.

import java.io.*;

public class login 
{
public static void main(String [] args) throws IOException
{
    String username = "admin"; 
    String password = "123";

    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Enter username : ");
    username = inData.readLine();

    System.out.print("Enter password : ");
    password = inData.readLine();

    if((username == "admin") && (password == "123"))
    {
        System.out.println("Welcome " + username + "\n*** Login Sucessfully ***" + "\n*** Access Granted ***");
    }
    else if((username != "admin") && (password == "123"))
    {
        System.out.println("Sorry, username is incorrect!\n*** Access Denied ***");
    }
    else if((username == "admin") && (password != "123"))
    {
        System.out.println("Sorry, password is incorrect!\n*** Access Denied ***");
    }
    else if((username != "admin") && (password != "123"))
    {
        System.out.println("Sorry, username and password is incorrect!\n*** Access Denied ***");
    }
}
}
Kero Kero
  • 9
  • 6

1 Answers1

0

.equals() method is used to compare the string in this case.
for e.g. username.equals("admin") , it compares the variable username's value with the value "admin" , here both are Strings

user219882
  • 15,274
  • 23
  • 93
  • 138
Kiran Kumar
  • 748
  • 5
  • 11
  • 34