It's been four days now and me being beginner I am I cant seem to make this work. So, so far my program would ask for user name and look for a match in a file I opened. If match found, it'll ask for userpassword and if userpassword is found in the file, it'll check username, password and if there is specific word on the line along with credentials, it'll open a credentials file. So I have my code working if username entered correctly the first time, it asks for the password and either breaks or lets the user to see the file. That works perfectly.
But if username is incorrect on the first try, it never checks if the second time is correct. It just ends it after 2 attempts (which is supposed to be 3 failed attempts). So here's my code
public static void main(String[] args)throws Exception {
Scanner scnr = new Scanner(System.in);
//open credentials file
FileInputStream in = new FileInputStream ("./credentials.txt");
Scanner credentials = new Scanner(in);
// open zookeeper file
FileInputStream in1 = new FileInputStream ("./zookeeper.txt");
Scanner zookeeperInfo = new Scanner(in1);
FileInputStream in2 = new FileInputStream ("./admin.txt");
Scanner adminInfo = new Scanner(in2);
FileInputStream in3 = new FileInputStream ("./veterinarian.txt");
Scanner vetInfo = new Scanner(in3);
String userName = "";
String userPassword = "";
String original = "";
int numAttempts = 0;
boolean run = true;
while (run) {
System.out.println ("User Name or Q: ");
userName = scnr.nextLine();
if (userName.equals("Q") || numAttempts > 3) {
run = false;
System.out.println("Goodbye..");
}
else {
while(credentials.hasNextLine()) {
String line = credentials.nextLine();
if (line.contains(userName)) {
System.out.println(userName);
System.out.println("Gimme the password: ");
userPassword = scnr.nextLine();
original = userPassword;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
if (line.contains(userName) && line.contains(sb.toString()) && line.contains("zookeeper")) {
while (zookeeperInfo.hasNextLine()) {
System.out.println(zookeeperInfo.nextLine() + " ");
}
break;
}
else if (line.contains(userName) && line.contains(sb.toString()) && line.contains("admin")) {
while (adminInfo.hasNextLine()) {
System.out.println(adminInfo.nextLine()+ " ");
}
break;
}
else if (line.contains(userName) && line.contains(sb.toString()) && line.contains("veterinarian")) {
while (vetInfo.hasNextLine()) {
System.out.println(vetInfo.nextLine() + " ");
}
break;
}
}
}
Picture of working part of the code Picture of non-working part
I really don't know. I feel like I am not even doing it correctly but all my tries end right here. I have to submit it by Sunday and after the whole week nothing works.. Please help, any advice will be greatly appreciated!
numAttemptsanywhere?credentialsand where do you incrementnumAttempts