JAVA Write a program to read a single password from an input
JAVA- Write a program to read a single password from an input file and print if that password is valid or invalid. The criteria for a valid password is as follows: 1. The password must be at least 8 characters long 2. The password must contain at least 1 digit (0-9) 3. The password must contain at least 1 uppercase letter 4. The password must contain at least 1 lowercase letter
Your program should contain two methods: 1) your main method, and 2) a second custom method to check if the password is valid.
Notes Each input file contains exactly one password. Your custom method should have a boolean return value. You will need to utilize some of Java’s String methods in this program. Some of these may include String.length(), String.toUpperCase(), and String.charAt().
It should also use the following code:
for (int i = 0; i < password.length(); i++) {
if ( Character.isDigit(password.charAt(i)) ) {
Solution
package password_validate;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class PasswordValidation {
public static boolean checkArguments(String password){ //method to return valid or invalid password. Takes password as input
boolean isDigit = false; //flag to check if there is a digit in the password
boolean isLowerca = false; //flag to check if there is a lowercase character in the password
boolean isUpperCa = false; //flag to check if there is a uppercase character in the password
if(password.length() < 8){ //check if the password length is less than 8. If so return false
return false;
}
for (int i = 0; i < password.length(); i++) { //run for loop for the password string
if ( Character.isDigit(password.charAt(i)) ) { // Check if there is a digit in the password string
isDigit = true;
}
else if( Character.isLowerCase(password.charAt(i)) ){ // Check if there is a lowercase character in the password string
isLowerca = true;
}
else if( Character.isUpperCase(password.charAt(i)) ){// Check if there is a an uppercase character in the password string
isUpperCa = true;
}
}
if(isDigit && isLowerca && isUpperCa){ //check if all the conditions meet and then return true
return true;
}
return false;
}
public static void main(String[] args) {
try (BufferedReader fileInput = new BufferedReader(new FileReader(args[0]))) { //read the file
String currentL;
while ((currentL = fileInput.readLine()) != null) { //read the content
//System.out.println(sCurrentLine);
boolean result = checkArguments(currentL); //call the checkArguments function and store the result in a variable
System.out.println(result); //print the result
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

