Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

(Java Programming) Currently I am running this code through NetBeans IDE and I'm getting an error message (posted below) and I do not know how

(Java Programming) Currently I am running this code through NetBeans IDE and I'm getting an error message (posted below) and I do not know how to fix it because I am far from an expert and I am not too clear on how to determine a fix from the NetBeans output. The code does work for one username, "rosario.dawson", but not others. Can you help me fix this error message and scan over the code to see if there are any other problems you would fix? Particularly, I'm not sure if the gathering of the user's role in the zoo is done correctly. Let me know if you see any problems with the program, and if you modify the program to work completely for you (not just to fix the error message) could you please send the code so I can study it. Thanks! EDIT: I've done some testing on my own... the code seems to work with user passwords that contain spaces (example: "alphabet soup", "animal doctor") however, unless the user's role is admin, it will not output the .txt document for their role. So I think these might be where the two bugs are, if you can confirm and tell me how to fix it? thank you!

____________________________________________________________________________________________________________________________________________________

Error Message:

image text in transcribed

____________________________________________________________________________________________________________________________________________________

authenticationsystem.java (MAIN):

package authenticationsystem;

import java.util.Scanner; import java.io.IOException; import java.security.NoSuchAlgorithmException;

public class AuthenticationSystem {

public static void main(String[] args) throws NoSuchAlgorithmException, Exception { Scanner scnr = new Scanner(System.in); int failedAttempts = 0; int maxAttempts = 3; boolean logOut; String userName; String userPass; UserInfo newUser = new UserInfo();

while (failedAttempts

if (newUser.userValidation(userName, userPass) == true) { newUser.getUserRole(); logOut = false; while (!logOut) { System.out.println(""); System.out.println("OPTIONS:"); System.out.println("Enter \"log out \" to return to log in screen."); System.out.println("Enter \"quit\" to close application."); switch (scnr.nextLine()) { case "log out": System.out.println("Logging out."); logOut = true; break; case "quit": System.out.println("Closing application."); System.exit(0); } } } else { System.out.println("Incorrect Username or Password."); ++failedAttempts; --maxAttempts; System.out.println("You have " + maxAttempts + " log in attempt(s) remaining."); }

if (failedAttempts == 3) { System.out.println("Log in attempts exceeded!"); System.out.println("Program closing."); System.exit(0); } } } } ____________________________________________________________________________________________________________________________________________________

userInfo.java:

package authenticationsystem;

import java.util.Scanner; import java.security.MessageDigest; import java.io.IOException; import java.io.FileInputStream; import java.security.NoSuchAlgorithmException; import java.io.File; import java.io.FileNotFoundException;

public class UserInfo {

private String userName; private String userPass; private String userRole;

public String getHashedPass(String password) throws NoSuchAlgorithmException { String original = password; MessageDigest md = MessageDigest.getInstance("MD5"); md.update(original.getBytes()); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b & 0xff)); } this.userPass = sb.toString(); return userPass; }

public boolean userValidation(String user, String password) throws IOException { File credFile = new File("src/authenticationsystem/credentials.txt"); Scanner credInfo = new Scanner(credFile); String validUser, validPass;

while (credInfo.hasNextLine()) { Scanner credLine = new Scanner(credInfo.nextLine()); validUser = credLine.next(); validPass = credLine.next(); credLine.next(); userRole = credLine.next(); if (validUser.equals(user) && validPass.equals(password)) { userName = user; userPass = password; userRole = credLine.next(); return true; }

} return false; } public String getUserPos() { return userRole; }

public void getUserRole() throws FileNotFoundException { if (this.getUserPos().equals("admin")) { File adminFile = new File("src/authenticationsystem/admin.txt"); Scanner adminInfo = new Scanner(adminFile); while (adminInfo.hasNextLine()) { System.out.println(adminInfo.nextLine()); } if (this.getUserPos().equals("veterinarian")) { File vetFile = new File("src/authenticationsystem/veterinarian.txt"); Scanner vetInfo = new Scanner(vetFile); while (vetInfo.hasNextLine()) { System.out.println(vetInfo.nextLine()); } if (this.getUserPos().equals("zookeeper")) { File zookeepFile = new File("src/authenticationsystem/zookeeper.txt"); Scanner zookeepInfo = new Scanner(zookeepFile); while (zookeepInfo.hasNextLine()) { System.out.println(zookeepInfo.nextLine()); } } } } } }

____________________________________________________________________________________________________________________________________________________

credential.txt

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

____________________________________________________________________________________________________________________________________________________

admin.txt

Hello, System Admin!

As administrator, you have access to the zoo's main computer system. This allows you to monitor users in the system and their roles.

____________________________________________________________________________________________________________________________________________________

veterinarian.txt

Hello, Veterinarian!

As veterinarian, you have access to all of the animals' health records. This allows you to view each animal's medical history and current treatments/illnesses (if any), and to maintain a vaccination log.

____________________________________________________________________________________________________________________________________________________

zookeeper.txt

Hello, Zookeeper!

As zookeeper, you have access to all of the animals' information and their daily monitoring logs. This allows you to track their feeding habits, habitat conditions, and general welfare.

____________________________________________________________________________________________________________________________________________________

NetBeans pictures of code layout:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

____________________________________________________________________________________________________________________________________________________

Thanks for the help!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions