In: Computer Science
Search the web to discover the 10 most common user-selected passwords, and store them in an array. Design a JAVA program that prompts a user for a password, and continue to prompt the user until the user has not chosen one of the common passwords.
Solution:
Look at the code and comments for better understanding...............
Screenshot of the code:
Output:
Code to copy:
import java.util.Scanner;
class Solution
{
//Store the 10 most common passwords in the array
//I obtained the passwords from internet
private static String[] common_pass = {"123456","123456789","qwerty","password","111111",
"12345678","abc123","1234567","password1","12345"};
//a function that takes a password and checks if it is present in the array or not
public static boolean isNotCommon(String password)
{
for(int i=0;i<common_pass.length;i++)
{
//if the password is present in the array return false
if(common_pass[i].equals(password))
return false;
}
//return true if the password is not in the array
return true;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String password;
//read untill user enters a un common password
while(true){
System.out.print("Enter a Password : ");
password = sc.nextLine();
//if password is not common break
if(isNotCommon(password))
{
System.out.println("Password Accepted..");
break;
}
System.out.println("Password is too common..");
}
}
}
I hope this would help.........................:-))
If you have any doubts please let me know in the comments...............:-))