0

I am just learning HashMaps, and have just written my first program using them. For some reason, my check to determine if the inputs I've entered match up with the key and it's corresponding value always returns false. Can anyone tell me why that is?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

public class Exercise {
    public static void main(String[] args) throws FileNotFoundException {
        HashMap<String, String> userPass = new HashMap<String,String>();
        HashMap<String, String> userFull = new HashMap<String, String>();
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the filename to read from: ");
        String filename = keyboard.nextLine();

        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        while (inputFile.hasNext()){
            String fullname=inputFile.next()+" "+inputFile.next();
            String username=inputFile.next();
            String pass=inputFile.nextLine();
            userPass.put(username, pass);
            userFull.put(username, fullname);
        }

        inputFile.close();

        //initialize variable for use after loop
        String inputUsr = null;
        //checks if key/value is found
        boolean b=false;
        int tries=1;
        while(b==false){
            System.out.print("Login: ");
            inputUsr=keyboard.next();
            System.out.print("\nPassword: ");
            String inputPass=keyboard.next();
            //if inputted password equals the password of the inputted username
            if(inputPass.equals(userPass.get(inputUsr)))
                b=true;
            System.out.println("Either the username or password is incorrect. You have "+(3-tries)+" more attempts.");
            tries++;
            //program quits afte 3 tries
            if(tries>3){
                System.exit(0);
            }
        }
        System.out.println("Welcome "+userFull.get(inputUsr));
    }
}
Seth Myers
  • 43
  • 1
  • 1
  • 8

1 Answers1

1

There are two problems in the code inside your while loop as explained below:

(1) keyboard.next() is reading the console output text i.e., reading the printed text 'Password', so replace keyboard.next() with keyboard.nextLine();

(2) You did not handle the else condition for the tries count

You can refer at the below code with inline comments:

while(b==false){
       System.out.print("Login: ");
       inputUsr=keyboard.nextLine();
       System.out.print("\nPassword: ");
       String inputPass=keyboard.nextLine();
       if(inputPass.equals(userPass.get(inputUsr))) {
           b=true;
       } else { 
          System.out.println("Either the username
            or password is incorrect. 
                      You have "+(3-tries)+" more attempts.");
                    tries++;
        }
        if(tries>3){
            System.exit(0);
        }
}
developer
  • 19,553
  • 8
  • 40
  • 57
  • Thank you, but those didn't fix the problem..when I was testing I printed the values right after they're created and they are correct, but it still always returns false for inputPass.equals(userPass.get(inputUsr)) – Seth Myers Dec 02 '16 at 23:53
  • nevermind, its fixed. it was the nextLine() call in the while loop at the top. Thank you! – Seth Myers Dec 02 '16 at 23:55