0

I got this code below to do a simple password (hashed) check function. But I encountered a problem, the code seem to work only for single line data in the file, the check works for Line1 but not Line2, I am unsure what is wrong. The data is shown below

the result is supposed to be hashedP matching either Line1 or 2. But it end up matching Line1 only

260670134225f2a24b59121739fec73584b0ddb6b49c39e31bd1df5483ac144d //Line1
cf80cd8aed482d5d1527d7dc72fceff84e6326592848447d2dc0b0e87dfc9a90 //Line2

Code:

public static void LoginMenu() {
    System.out.println("Please Enter Your Password: ");
    Scanner UserPass = new Scanner(System.in);
    String UserP = UserPass.nextLine();
    String hashedP = Utility.getHash(UserP);

    File file = new File("admin.dat");
    try {
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
            String fileline = scanner.nextLine();
            if (!(fileline.equals(hashedP))) {
                System.out.println("Login Failed!");
                LoginMenu();
            }
            else {
                System.out.println("Login Successful.\n");
                AdminMenu();
            }
        }
        scanner.close();
    }
    catch (FileNotFoundException exc) {
        exc.printStackTrace();
    }
}
2
  • What exactly is your expected output and what is the actual output? Commented Apr 6, 2015 at 10:29
  • The expected output is supposed to be hashedP matching any line in the file but it ended up matching only Line1 Commented Apr 6, 2015 at 11:28

1 Answer 1

2

Let's analyse this section:

while (scanner.hasNextLine()) {
    String fileline = scanner.nextLine();
    if (!(fileline.equals(hashedP))) {
        System.out.println("Login Failed!");
        LoginMenu();
    }
    else {
        System.out.println("Login Successful.\n");
        AdminMenu();
    }
}
  1. We enter the while loop.
  2. We read the first line from the file.
  3. We check it against hashedP.
    3.1. If it matches, we display the admin screen.
    3.2. If it does not match, we prompt the user to log in again.

You never even reach the second line in the file, it fails too fast.
You should refactor your loop to try harder:

boolean failed = true;
while (scanner.hasNextLine())
{
    String fileline = scanner.nextLine();
    if (fileline.equals(hashedP))
    {
        failed = false;
        System.out.println("Login Successful.\n");
        AdminMenu();
    }
}
if(failed)
{
    System.out.println("Login Failed!");
    LoginMenu();
}

Unrelated to that, it's never a good idea to call a function recursively if in any way avoidable.
In this case, for example, admin.dat and a new stdin Scanner will be opened as many times as an incorrect password is entered, which is poorly designed.
I suggest using a while(true) loop instead to read the password and doing everything else before that:

public static void LoginMenu()
{
    ArrayList<String> hashes = new ArrayList<String>();
    File file = new File("admin.dat");
    try
    {
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine())
        {
            hashes.add(scanner.nextLine());
        }
        scanner.close();
    }
    catch (FileNotFoundException exc)
    {
        exc.printStackTrace();
        return;
    }
    Scanner UserPass = new Scanner(System.in);
    while (true)
    {
        System.out.println("Please Enter Your Password: ");
        String UserP = UserPass.nextLine();
        String hashedP = Utility.getHash(UserP);
        if (hashes.contains(hashedP))
        {
            System.out.println("Login Successful.\n");
            AdminMenu();
            break;
        }
        System.out.println("Login Failed!");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.