Java login system doesn't work

84 Views Asked by At

I can't get this login system to work. I want the program when run to display,

Username:

Password:

and I want to then enter credentials.

Here is my code:

import java.util.Scanner;

public class Customer {


    public  void Login() {
Scanner sc = new Scanner(System.in);

System.out.print("SUPERMARKET - ONLINE PORTAL LOGIN \n");


System.out.print("Username: ");
System.out.print("\n");

System.out.print("Password: ");

String string = sc.nextLine();
if("hmirza".equals(string) )
{
    String string2 = sc.nextLine();
    if("mirza".equals(string2) )
    {
        System.out.print("Logging you in... ");
        System.out.print("\n\n\n");
        new Products().search();


}
    } 
        sc.close();
    }

} 
1

There are 1 best solutions below

1
Maxim Kasyanov On

Try this:

public void Login() {
    Scanner sc = new Scanner(System.in);

    System.out.print("SUPERMARKET - ONLINE PORTAL LOGIN \n");


    System.out.print("Username: ");
    String username = sc.nextLine();
    System.out.print("\n");
    System.out.print("Password: ");
    String password = sc.nextLine();
    if ("hmirza".equals(username)) {
      if ("mirza".equals(password)) {
        System.out.print("Logging you in... ");
        System.out.print("\n\n\n");
        new Products().search();
      }
    }
    sc.close();
  }