234k views
0 votes
50 PTS

in Java
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!

1 Answer

3 votes

import java.util.Scanner;

public class JavaApplication46 {

public static boolean passwordCheck(String pass){

String alphanum = "abcdefghijklmnopqrstuvwxyz1234567890";

boolean legit = true;

if (pass.length()>= 8){

for(int i = 0; i < pass.length(); i++){

if (alphanum.indexOf(pass.charAt(i)) != -1 && legit != false){

legit = true;

}

else{

legit = false;

}

}

}

else{

legit = false;

}

return legit;

}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Enter a password: ");

String password = scan.next();

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

}

}

I hope this helps!

User Priyanka V
by
5.4k points