Hallo there! I need help in this quation, this is programering in java. My Task!
ID: 3919528 • Letter: H
Question
Hallo there!
I need help in this quation, this is programering in java.
My Task!
Write a method like public static char [] generatePassword () {} that creates and returns an array of char that represents a password according to the following rules:
• Is between 8 -12 characters long
• Contains at least one digit 0-9,
• Contains at least one upper case letter,
• Contains at least one lowercase letter
Tip! Fill an array with all allowed characters. Then let the method select a certain number of characters in a swapping order and number from the array.
write a main () that shows that method works as expected. How many of the generated passwords are correct?
Explanation / Answer
package rental;
import java.util.Scanner;
public class Password {
public static char [] generatePassword ()
{
System.out.println("Password must meet these requirements." +
" Must have length 8 - 12 Characters. Must contain 1 lower case letter." +
" Must contain 1 upper case letter. Must contain 1 numeric digit.");
//Scanner class for taking input
Scanner sc=new Scanner(System.in);
String passwords;
System.out.print("Enter password : ");
passwords=sc.next();
//converting String password to char array
char[] password= passwords.toCharArray();
if(password.length<8 && password.length>12 )
{
System.out.println("Invalid password - Must have length 8 - 12 Characters.");
password= null;
}
// checking integer
else if(!containsInt(password))
{
System.out.println("Invalid password - Must have an Integer.");
password= null;
}
//Checking upper case Character
else if(!containsUpper(password))
{
System.out.println("Invalid password - Must have an Upper Case character.");
password= null;
}
//Checking lower case Character
else if(!containsLower(password))
{
System.out.println("Invalid password - Must have a Lower Case character.");
password= null;
}
return password;
}
//Method for checking integer
public static boolean containsInt(char[] array)
{
boolean ifExists=false;
for(char chars:array)
{
int c=Character.getNumericValue(chars);
for(int i=0;i<10;i++)
{
if(c==i)
{
ifExists=true;
}
}
}
return ifExists;
}
//Method for checking Upper case character
public static boolean containsUpper(char[] array)
{
boolean ifExists=false;
for(char chars:array)
{
if(Character.isUpperCase(chars))
{
ifExists=true;
}
}
return ifExists;
}
//Method for checking lower case character
public static boolean containsLower(char[] array)
{
boolean ifExists=false;
for(char chars:array)
{
if(Character.isLowerCase(chars))
{
ifExists=true;
}
}
return ifExists;
}
//Main method for call generatePassword () method
public static void main(String[] args) {
char[] printPassword=generatePassword () ;
//Printing password
System.out.print("Password is: ");
//Handling NullPointerException in case of not entering valid password
try {
for(char password:printPassword)
{
System.out.print(password);
}
}catch(NullPointerException e)
{
System.out.println("Enter a valid Password");
}
}
}
Output is:Password must meet these requirements.
Must have length 8 - 12 Characters.
Must contain 1 lower case letter.
Must contain 1 upper case letter.
Must contain 1 numeric digit.
Enter password : javaProgram1
Password is: javaProgram1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.