Question: I had to create a file called badpasswors.txt in which I put random passwords that cannot work. Example escape123, AustinDerr333, etc. How do I make
I had to create a file called badpasswors.txt in which I put random passwords that cannot work. Example escape123, AustinDerr333, etc. How do I make this code read the passwords I put in the badpassword file and make it work so that when the user puts one of those passwords in it will not work and ask for password again. I think I got the file to imput to code but not sure if it is actually reading whats on the file. Very confused. Can someone type out what I need and where in this code. Thank You!!!
import java.util.Scanner; import java.io.File; import java.io.*;
public class ProgramTwo {
public static void main(String[] args) {
// TO DO code application logic here //scanner object Scanner sc = new Scanner(System.in);
System.out.println("Enter your first name: "); String fname = sc.nextLine(); System.out.println("Enter your last name: "); String lname = sc.nextLine();
//appending the first character String username = lname + fname.charAt(0);
if(username.length() < 5){ //appending second character username += fname.charAt(1); } username = username.toLowerCase(); String password;
while(true){ System.out.println("Enter a password: "); password = sc.nextLine(); //flag variables for keeping track of all the requirements int len_flag = 0, upper_flag = 0, lower_flag = 0, digit_flag = 0; int punct_flag = 0; //checking length if(password.length() >= 8) len_flag = 1;
for(int i = 0; i < password.length(); i++){ //check if it is a upper case if(Character.isUpperCase(password.charAt(i))){ upper_flag = 1; } //check if it is a lower case if(Character.isLowerCase(password.charAt(i))){ lower_flag = 1; } //check if it is a digit if(Character.isDigit(password.charAt(i))){ digit_flag = 1; } int ascii = password.charAt(i);
//check if it is a punctuation if(ascii >= 33 && ascii <= 47) punct_flag = 1;
} //used to keep track if password exists in the file int file_flag = 0; try{ File file = new File("badpasswords.txt"); Scanner scan = new Scanner(new File("badpasswords.txt"));
//loop till there exists a new line in the file while (scan.hasNextLine()){ try{ //load the password from file String temp_password = scan.nextLine(); temp_password = temp_password.toLowerCase(); //compare to check if it matches with the entered password if(password.equals(temp_password)){ file_flag = 1;
} //System.out.println(temp_password); } catch(Exception e){
break; }
}
} catch(IOException exception){ System.out.println("can not read from file"); System.exit(-1); }
//break if all the conditions are met if(file_flag == 0 && upper_flag == 1 && lower_flag == 1 && digit_flag == 1 && punct_flag == 1){ System.out.println("Successfully set the password!"); System.out.println("Username: "+username); break; }
}
}
}
//output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
