Here is my program: import java.util.Scanner; // Needed for the Scanner class pu
ID: 3553675 • Letter: H
Question
Here is my program:
import java.util.Scanner; // Needed for the Scanner class
public class PasswordVerifier {
public static void main(String[] args) {
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
String input;// To hold the input
// Ask the user to enter their password
System.out.print("Please enter your password: ");
input = keyboard.nextLine();
// Demonstrate the method by passing the inputted string.
if (authenticate(input))
System.out.print("Valid password. You may proceed");
else
System.out.print("This password is invalid");
System.out.println();
}
//Method: Accepts a String object (password) as an argument
//and verifies whether it meets the criteria of a valid password.
private static boolean authenticate(String pass)
{
// The password should be at least six characters long.
// The password should contain at least one uppercase
// and at least one lowercase letter.
// The password should have at least one digit.
return (pass.length() == 6) &&
(pass.matches("[A-Z]")) &&
(pass.matches("[a-z]")) &&
(pass.matches("[0-9]"));
}
}
The program does not work. There are no errors, but the password is ALWAYS invalid. How do I fix it?
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Pattern;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
String input;// To hold the input
// Ask the user to enter their password
System.out.print("Please enter your password: ");
input = keyboard.nextLine();
// Demonstrate the method by passing the inputted string.
if (authenticate(input))
System.out.print("Valid password. You may proceed");
else
System.out.print("This password is invalid");
System.out.println();
}
private static boolean authenticate(String pass)
{
// The password should be at least six characters long.
// The password should contain at least one uppercase
// and at least one lowercase letter.
// The password should have at least one digit.
return (pass.length() >= 6) &&
(Pattern.compile("[0-9]").matcher(pass).find()) &&
(Pattern.compile("[A-Z]").matcher(pass).find()) &&
(Pattern.compile("[a-z]").matcher(pass).find());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.