open text file after user login java -


i working on project school due tonight , have exhausted resources. required build authentication program zoo allows employee login limited attempts. after successful login suppose display text file according role in fourth column.

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 

i have successful login, limited 3 attempts, , option logout , exit, can't seem figure out why isn't loading role files accordingly.. on appreciated!

import java.io.file; import java.nio.charset.charset; import java.nio.file.files; import java.nio.file.paths; import java.security.messagedigest; import java.util.list; import java.util.scanner;   public class authenticationsystem {     public static void main(string args[]) {         try {             scanner scan;             scan = new scanner(new file("c:\\users\\zane hewgley\\desktop\\authsys\\.idea\\credentials.txt"));             string credentials[][] = new string[100][4];             int count = 0;             while (scan.hasnextline()) { //read file                 credentials[count][0] = scan.next();                 credentials[count][1] = scan.next(); //obtain credentials file                 string l[] = scan.nextline().split("\"[ ]+");                 l[0] = l[0].trim();                 l[0] = l[0].replace("\"", "");                  credentials[count][2] = l[0];                 credentials[count][3] = l[0].trim();                 count++;             } //request user input             scanner input = new scanner(system.in);             boolean run = true;             int tries = 0;              while (run) {                 system.out.println("-welcome-");                 system.out.println("1-login");                 system.out.println("2-exit");                  int ch = integer.parseint(input.nextline().trim());                  if (ch == 1) { //increment number of attempts                     tries++; //request username , password                     system.out.print("username: ");                     string username = input.nextline();                     system.out.print("password: ");                     string password = input.nextline(); //generate hash                     messagedigest md;                     md = messagedigest.getinstance("md5");                     md.update(password.getbytes());                     byte[] digest = md.digest();                     stringbuilder sb = new stringbuilder();                     (byte b : digest) {                         sb.append(string.format("%02x", b & 0xff));                     }                     string hpassword = sb.tostring();                      boolean baduser = true;                     (int = 0; < count; i++) {                         if (username.contentequals(credentials[i][0])) {                             if (hpassword.contentequals(credentials[i][1])) { //if verified, logged in                                 list<string> data = null; //check type of user , print                                 switch (credentials[i][3]) {                                     case "zookeeper":                                         data = files.readalllines(paths.get("zookeeper.txt"), charset.defaultcharset());                                         break;                                     case "admin":                                         data = files.readalllines(paths.get("admin.txt"), charset.defaultcharset());                                         break;                                     case "veterinarian":                                         data = files.readalllines(paths.get("veterinarian.txt"), charset.defaultcharset());                                         break;                                     default:                                         break;                                 }                                 if (data != null) {                                     (string s : data) {                                         system.out.println(s);                                     }                                 } //reset                                 tries = 0;                                  system.out.println("\n1) logout.");                                 system.out.println("2) exit.");                                  ch = integer.parseint(input.nextline().trim());                                 if (ch == 2) {                                     run = false;                                 }                                 baduser = false;                                 break;                             }                         }                     }                     if (baduser) {                         system.out.println("invalid username or password.");                     }                 } else {                     break;                 } //limit attempts                 if (tries == 3) {                     run = false;                     system.out.println("immoderate attempts!");                 }             }         } catch (exception ex) {             ex.printstacktrace();         }     } } 

when split array information:

        string l[] = scan.nextline().split("\"[ ]+");         l[0] = l[0].trim();         l[0] = l[0].replace("\"", ""); 

you go on write result - password - in credentials[count][3]; later contain password when check if zookeper, etc.

above have fixed lengths; if use that, split line this:

        while (scan.hasnextline()) {             string line = scan.nextline();             credentials[count][0] = line.substring(0, 20).trim();             credentials[count][1] = line.substring(20, 55).trim();             credentials[count][2] = line.substring(55, 74).trim();             credentials[count][3] = line.substring(74).trim();             count++;         } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -