-1

The program allows the user to enter a string representing a password and determines whether or not it is valid. The password must be 8 characters long, include one lower case letter, one upper case letter, a number, and a symbol (ex. !@#).

The output should read:

Entered Password: (whatever the user entered)
Verdict: (either valid or invalid)

Here is my code so far:

import java.util.*;
public class PasswordTest
{
   public static void main(String[]args)
   {
      Scanner input = new Scanner (System.in);

      System.out.print("Enter a password: ");
      String Pass = input.next();

      System.out.println("Entered Password: " + Pass);

      if (Pass.length() < 8)
      {
         System.out.println ("Verdict: Invalid");
      }
      if (Pass.length() >= 8)
      {
         System.out.println ("Verdict: Valid");
      { 
   }
}

I'm not certain how to go about this. I'm assuming I'll need a loop statement to determine whether or not it contains a capital and lowercase letter as well as a number and symbol.

BasdGod
  • 33
  • 2
  • 8
  • on a side note, try using the [Console#readPassword](http://docs.oracle.com/javase/7/docs/api/java/io/Console.html#readPassword%28%29) to read passwords from the input devices. This minimizes the lifetime of sensitive data like password in memory, lessening the chances of attacks. – asgs Oct 03 '14 at 05:20

2 Answers2

0

No need for loops, use || and && operators instead, that way only 1 if statement is needed.

CaffeineToCode
  • 830
  • 3
  • 13
  • 25
0

You can use regex:

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\S+$).{8,}$

Implementation:

import java.util.regex.*;

public class passwordvalidation {
  public static void main(String[] args) {
    String passwd = "aaZZa44@"; 
    String pattern = "(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{5,10}";
    System.out.println(passwd.matches(pattern));
  }
}

Find more here in this stackoverflow post: Regex explained

Community
  • 1
  • 1
MSA
  • 2,502
  • 2
  • 22
  • 35