I need help figuring out how to get this to let the user input a username and password for 3 tries. On the third failure it will alert the user and close the program.
I think I need a for loop, but I am not good with them. help please?
Code
public class JavaAuthentication {
private static Scanner x;
public static void main(String[] args) {
// TODO code application logic here
String username = "rosario.dawson";
String password = "animal doctor";
String filepath = "credentials.txt";
verifyLogin(username, password, filepath);
System.out.println("Finish");
}
public static void verifyLogin(String username, String password, String filepath){
//Define the variables used in proving username/password
boolean found = false;
String tempUsername;
String tempPassword;
//this block searches for the filepath and defines a method of
//grabbing the temp username/password
try{
x = new Scanner(new File(filepath));
x.useDelimiter("[,\n]");
//While loop to find the next set of characters to set to temp username/password
while(x.hasNext() && !found){
tempUsername = x.next();
tempPassword = x.next();
//Compares the scanned files temp username/password to the user input username/password
if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())) {
found = true;
System.out.println("Authentication successful!");
}
}
if(found == false){
System.out.println("Invalid Username and/or Password.");
}
x.close();
}
//Displays "Error!" is anything goes wrong in the method
catch(Exception e){
System.out.println("Error!");
}
}
}