samedi 15 décembre 2018

JAVA if statement is also printing else statement

I have a program where a user is asked to input a username and a password which is converted to an M5D hash. These two inputs are then tested to see if they match the fields in a .txt file. There are also 3 roles which are tested, and depending on which role corresponds with the user, the contents of another .txt file will be displayed. My problem is when I have a correct username, password, and role. The role file will open, but it will also print "Incorrect Info" which is meant to print only if the username or password do not match.

Contents of text file:

griffin.keyes   108de81c31bf9c622f76876b74e9285f    "alphabet soup" zookeeper
rosario.dawson  3e34baa4ee2ff767af8c120a496742b5    "animal doctor" admin
bernie.gorilla  a584efafa8f9ea7fe5cf18442f32b07b    "secret password"   veterinarian
donald.monkey   17b1b7d8a706696ed220bc414f729ad3    "M0nk3y business"   zookeeper
jerome.grizzlybear  3adea92111e6307f8f2aae4721e77900    "grizzly1234"   veterinarian
bruce.grizzlybear   0d107d09f5bbe40cade3de5c71e9e9b7    "letmein"   admin

What my output looks like:

Output when running with correct credentials

As you can see, it prints "incorrect" after opening the corresponding .txt file because the credentials were correct. Why is it printing the else statement and how can I fix it? Thanks.

AuthenticationSystem.java (main method)

package authenticationsystem;

import java.io.*;
import java.util.Scanner;


public class AuthenticationSystem
{
public static void openZoo()
{
    String line = null;
    try
    {
        FileReader filereader = new FileReader("zookeeper.txt");
        try (BufferedReader bufferedReader = new BufferedReader(filereader)) {
            while ((line = bufferedReader.readLine()) != null)
            {
                System.out.println(line);
            }
            bufferedReader.close();
        }
    }

    catch (FileNotFoundException ex)
    {
        System.out.println("Unable to open file");
    }
    catch (IOException ex)
    {
        System.out.println("Error reading file");
    }
}



public static void openVet()
{
    String line = null;
    try
    {
        FileReader filereader = new FileReader("veterinarian.txt");
        try (BufferedReader bufferedReader = new BufferedReader(filereader)) {
            while ((line = bufferedReader.readLine()) != null)
            {
                System.out.println(line);
            }
            bufferedReader.close();
        }
    }

    catch (FileNotFoundException ex)
    {
        System.out.println("Unable to open file");
    }
    catch (IOException ex)
    {
        System.out.println("Error reading file");
    }

}

public static void openAdmin()
{
    String line = null;
    try
    {
        FileReader filereader = new FileReader("admin.txt");
        try (BufferedReader bufferedReader = new BufferedReader(filereader)) {
            while ((line = bufferedReader.readLine()) != null)
            {
                System.out.println(line);
            }
            bufferedReader.close();
        }
    }

    catch (FileNotFoundException ex)
    {
        System.out.println("Unable to open file");
    }
    catch (IOException ex)
    {
        System.out.println("Error reading file");
    }
} 

public static void main(String[] args) throws FileNotFoundException, IOException 
{
    String userName;
    String userPassword;
    Scanner scnr = new Scanner(System.in);
    int attempts = 0;

    while (attempts < 3)
    {
        System.out.println("Enter a username: ");
        userName = scnr.nextLine();
        System.out.println("Enter a password: ");
        userPassword = scnr.nextLine();

        String hashedPassword;
        HashGenerator hash = new HashGenerator();
        hashedPassword = hash.getMD5Hash(userPassword);
        String currentLine = null;


        FileReader fr = new FileReader("credentials.txt");
        BufferedReader br = new BufferedReader(fr);
        char exit = 'q';


        while ((currentLine = br.readLine()) != null)
        {
            String[] line = currentLine.split("\t");


            if (line[0].equals(userName) && line[1].equals(hashedPassword) && line[3].equals("zookeeper"))
                {
                    openZoo();
                }

            if (line[0].equals(userName) && line[1].equals(hashedPassword) && line[3].equals("admin"))
                {
                    openAdmin();
                }

            if (line[0].equals(userName) && line[1].equals(hashedPassword) && line[3].equals("veterinarian"))
                {
                    openVet();

                }

            else
            {
                System.out.println("Incorrect");
            }
















        }





    }
}

}

HashGenerator.java (Used to convert to M5D hash)

package authenticationsystem;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* This class implements hash functions.
* Currently, the class only supports MD5 hash codes.
*
*/
public class HashGenerator {

final String hashType = "MD5";

/**
 * Default constructor
 */
public HashGenerator() {

}

/**
 * This method will take a plain text password
 * and return a MD5 hash of that password.
 * @param password
 * @exception NoSuchAlgorithmException
 * @return String
 */
public String getMD5Hash(String password) {

    // This MessageDigest class provides applications
    // the functionality of a message digest algorithm
    MessageDigest md = null;
    String hashedPassword = null;
    StringBuffer sb = new StringBuffer();

    if (password != null) {
        try {
            md = MessageDigest.getInstance(hashType);
            md.update(password.getBytes());
            byte[] digest = md.digest();

            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }

            hashedPassword = sb.toString();
        } catch (NoSuchAlgorithmException nsae) {
            System.err.println(nsae);
        }
    }

    return hashedPassword;
}

}

Aucun commentaire:

Enregistrer un commentaire