Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using: import java.util.Scanner; public class Password { public static void main(String[] args) { // Prompt the user to enter a password Scanner input = new

Using:

import java.util.Scanner; public class Password { public static void main(String[] args) { // Prompt the user to enter a password Scanner input = new Scanner(System.in); System.out.print("Enter a string for password: "); String s = input.nextLine(); if (isValidPassword(s)) { System.out.println("Valid Password"); } else { System.out.println("Invalid Password"); } } /** Check if a string is a valid password */ isValidPassword(String s) { // Remember to complete the signature // write the method body here } }

Write this isValidPassword(String s) method with appropriate signature, including a return type. The method should check if the string parameter is a valid password using the following characteristics:

-A mixture of letters and numbers (alphanumeric)

-At least 8 characters long

-The number of digits must be at least 2

Use the following algorithm (pseudocode) to complete isValidPassword(String s) method:

//Checking for only letters or digits

Use for loop to loop through each character in string s:

within your loop, test if the current character is not a letter and is not a digit - You can use this if (!Character.isLetter(s.charAt(i)) && !Character.isDigit(s.charAt(i))) within your condition, return false

end the loop

//Checking for password length

Test if the length of String s is less than 8: if (s.length() < 8)

within your condition, return false

//Counting the number of digits

declare a counter variable as integer and initialize it to zero

Use for loop to loop through each character in string s:

within your loop, test if the current character is not a digit - You can use - if (Character.isDigit(s.charAt(i)))

within your condition, increment the counter variable

end of loop

//checking if the number of digits is two or more

Test if the counter variable is greater than or equal to 2

within your condition, return true

Otherwise - This is the else block

within your else block, return false

Here are the sample runs:

Enter a string for password: 23daniel Valid Password

Enter a string for password: daniel Invalid Password

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions