-2

For some reason I get an error that the line currentUser = gar.getAccount(userN); is unreachable and yet it shouldn't be. Garage.getAccount() is just a retrieval from a Hashmap. Both if statements don't counter each other and I've tried adding else statements but no matter what it says the line is unreachable.

package main;

import java.util.Scanner;

public class Main {

    private static Scanner input;
    private static Garage gar;
    private static Attendant currentUser;
    private static boolean isManager;

    public static void main(String[] args) {
        input = new Scanner(System.in);
        gar = new Garage(10, 80, 10);
        currentUser = null;
        while (currentUser == null)
            logIn();
    }

    public static void logIn() {
        System.out.println("Enter username: ");
        String userN = input.nextLine();
        System.out.println("Enter password:");
        String userP = input.nextLine();
        //if no username, go back
        if(gar.getAccount(userN) == null) { 
            error("Incorrect username");
            return;
        } 
        if(gar.getAccount(userN).getPassword().equals(userP) == false); { //if entered password doesn't match
            error("Incorrect password");
            return;
        } 
        currentUser = gar.getAccount(userN);
        return;
    }

    //update to throw error pop-up later
    public static void error(String er) { System.out.println(er); }
}
hooknc
  • 4,854
  • 5
  • 31
  • 60
  • 2
    `if(gar.getAccount(userN).getPassword().equals(userP) == false);` the semicolon terminates the `if` body. Remove it. Voting to close as typo. – Elliott Frisch Sep 29 '19 at 05:21

1 Answers1

0

Your Syntax is incorrect, Condition of if statement should not be ended with semicolon.

 package main;

    import java.util.Scanner;

    public class Main {

        private static Scanner input;
        private static Garage gar;
        private static Attendant currentUser;
        private static boolean isManager;

        public static void main(String[] args) {
            input = new Scanner(System.in);
            gar = new Garage(10, 80, 10);
            currentUser = null;
            while (currentUser == null)
                logIn();
        }

        public static void logIn() {
            System.out.println("Enter username: ");
            String userN = input.nextLine();
            System.out.println("Enter password:");
            String userP = input.nextLine();
            //if no username, go back
            if(gar.getAccount(userN) == null) { 
                error("Incorrect username");
                return;
            } 
            if(gar.getAccount(userN).getPassword().equals(userP) == false) { //if entered password doesn't match
                error("Incorrect password");
                return;
            } 
            currentUser = gar.getAccount(userN);
            return;
        }

        //update to throw error pop-up later
        public static void error(String er) { System.out.println(er); }
    }
Sagar Timalsina
  • 191
  • 3
  • 14