39.0k views
3 votes
Write a program with a method called passwordCheck to return if the string is a valid password. The method should have the signature shown in the starter code.

The password must be at least 8 characters long and may only consist of letters and digits. To pass the autograder, you will need to print the boolean return value from the passwordCheck method.
Hint: Consider creating a String that contains all the letters in the alphabet and a String that contains all digits. If the password has a character that isn’t in one of those Strings, then it’s an illegitimate password!
Coding portion:
public class Password
{
public static void main(String[] args)
{
// Prompt the user to enter their password and pass their string
// to the passwordCheck method to determine if it is valid.
}
public static boolean passwordCheck(String password)
{
// Create this method so that it checks to see that the password
// is at least 8 characters long and only contains letters
// and numbers.
}
}

User Jebeaudet
by
4.0k points

1 Answer

4 votes

Answer:

import java.util.regex.*;

import java.util.Scanner;

public class Password

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Please enter the Password. \\");

String passwordString = input.next();

System.out.println(passwordCheck(passwordString));

}

public static boolean passwordCheck(String password){

if(password.length()>=8 && password.matches("[a-z0-9A-Z]+")){

return true;

}

else{

return false;

}

}

}

Step-by-step explanation:

The Java class "Password" is used by the program to make an instance of the password object and check its validity with the passwordChecker method defined within the Password class. The passwordChecker method only matches alphanumeric passwords, that is, passwords with alphabets and numbers. If a non-alphanumeric character is encountered, the boolean value false is returned.

User Boris Schegolev
by
4.7k points