Question

i need know how Write a program that tests whether a string is a valid password....

i need know how Write a program that tests whether a string is a valid password. The password rules are:

  • It must have at least eight characters.
  • It must contain letters AND numbers
  • It must contain at least two digits
  • It must contain an underscore (_)

Write a program that prompts the user to enter a password and displays the password entered and whether it is valid or invalid.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment.

Program

import java.util.Scanner;

public class Test {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the password: ");
       String pass = sc.nextLine();
       System.out.println(pass+" is valid? "+checkPassword(pass));
      
      
       /*Test cases for code verfication*/
//       System.out.println("asdzx_12 is valid? "+checkPassword("asdzx_12"));
//       System.out.println("asdzxs_1a is valid? "+checkPassword("asdzxs_1a"));
//       System.out.println("johnw_7_ is valid? "+checkPassword("johnw_7_"));
      
       sc.close();
      
   }
  
   public static boolean checkPassword(String pass) {
       int digits=0, letters=0, underscore=0;
       char[] ch = pass.toCharArray();
      
       if(pass.length()<8)//return falses if length is less than 8
           return false;
       else {
          
           for(int i=0; i< ch.length; i++) {
               //checking number of letters, digits and underscore
               if(Character.isDigit(ch[i]))
                   digits++;
               if(Character.isLetter(ch[i]))
                   letters++;
               if(ch[i] == '_')
                   underscore++;
           }
           //validating password
           if(letters>0 && digits>=2 && underscore==1)
               return true;
           else
               return false;
       }
      
   }
}

Output

Add a comment
Know the answer?
Add Answer to:
i need know how Write a program that tests whether a string is a valid password....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

  • Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter charact...

    Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter character • Contains at least 1 upper letter character • Contains at least 1 numeric digit • Contains at least 1 special character from the set: !@#$%^&* • Does not contain the word “and” or the word “the” Prompts the user for a password, including the requirements above in your output. Output a string that states valid or invalid. If invalid, state which...

  • Write a program that asks the user to enter a password, and then checks it for...

    Write a program that asks the user to enter a password, and then checks it for a few different requirements before approving it as secure and repeating the final password to the user. The program must re-prompt the user until they provide a password that satisfies all of the conditions. It must also tell the user each of the conditions they failed, and how to fix it. If there is more than one thing wrong (e.g., no lowercase, and longer...

  • in C++, Design a program that asks the user to enter a password, and then analyze...

    in C++, Design a program that asks the user to enter a password, and then analyze the password, and then analyze the password for the following weaknesses: 1. Fewer than 8 characters 2. Does not contain at least one uppercase letter and one lowercase latter 3. Does not contain at least one numeric digit 4. Does not contain at least one special character( a character that is not a letter or numeric digit) 5. Is a sequence of consecutive uppercase...

  • Design and implement a Java program (name it PasswordTest) that accepts a string from the user...

    Design and implement a Java program (name it PasswordTest) that accepts a string from the user and evaluates it as a password, giving it a valid or invalid verdict A good password will be at least 8 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order...

  • This is another attempt to ask this question: I need a Java program that asks the...

    This is another attempt to ask this question: I need a Java program that asks the user for a String. the string must be atleast 7 character and only words and/or numbers, the program should read this input and output “Valid”. No special characters or whitespaces are allowed, if so output “Invalid” *** Please note: The program must be able to know if the string is less than 7 characters and output INVALID. Also the program must be able to...

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • Can I please get some assistance on this? 1. Write a program to test the correctness...

    Can I please get some assistance on this? 1. Write a program to test the correctness of a password according to some rules. If the password is not valid, print messages showing what's wrong with the password. Partial program output should look like this: Please enter a password. It must be at least 8 characters long and contain at least 2 alpha characters, at least one upper case and one lower case, at least 1 numbers, and at least 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT