Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help with making a password checker in java

I am trying to make a program in Java that checks for three specific inputs. It has to have pass these tests:

  1. At least 7 characters.
  2. Contain both upper and lower case alphabetic characters.
  3. Contain at least 1 digit.

So far I have been able to make it check if there is 7 characters, but I am having trouble with the last two. What should I put in my loop as an if statement to check for digits and make it upper and lower case. Any help would be greatly appreciated. Here is what I have so far.

import java.awt.*;
import java.io.*;
import java.util.StringTokenizer;

public class passCheck
{


    private static String getStrSys ()
    {
        String myInput = null;          //Store the String that is read in from the command line

        BufferedReader mySystem;        //Buffer to store the input

        mySystem = new BufferedReader (new InputStreamReader (System.in)); //creates a connection to system input
        try
        {
            myInput = mySystem.readLine (); //reads in data from the console
            myInput = myInput.trim ();
        }
        catch (IOException e)  //check
        {
            System.out.println ("IOException: " + e);
            return "";
        }
        return myInput; //return the integer to the main program
    }


    //****************************************
    //main instructions go here
    //****************************************

    static public void main (String[] args)
    {

        String pass;         //the words the user inputs
        String temp = "";            //holds temp info

        int stringLength;          //length of string
        boolean goodPass = false;


        System.out.print ("Please enter a password: ");  //ask for words

        pass = getStrSys ();                              //get words from system
        temp = pass.toLowerCase ();
        stringLength = pass.length ();             //find length of eveyrthing



        while (goodPass == false)

            {
                if (stringLength < 7)
                {
                    System.out.println ("Your password must consist of at least 7 characters");
                    System.out.print ("Please enter a password: ");  //ask for words
                    pass = getStrSys ();
                    stringLength = pass.length ();
                    goodPass = false;
                }
                else if (/* something to check for digits */)
                {

                }

            }
like image 833
Cheesegraterr Avatar asked Apr 03 '10 02:04

Cheesegraterr


1 Answers

Sure you can come up with a convoluted—almost unreadable—regex for doing this but I wouldn't suggest it. Apart from the readability aspect, if the password fails it doesn't tell you why. This solves both of these problems:

while (true) {
  pass = getStrSys();
  if (pass.length() < 7) {
    System.out.println("must be at least 7 characters long");
  } else {
    boolean upper = false;
    boolean lower = false;
    boolean number = false;
    for (char c : pass.toCharArray()) {
      if (Character.isUpperCase(c)) {
        upper = true;
      } else if (Character.isLowerCase(c)) {
        lower = true;
      } else if (Character.isDigit(c)) {
        number = true;
      }
    }
    if (!upper) {
      System.out.println("must contain at least one uppercase character");
    } else if (!lower) {
      System.out.println("must contain at least one lowercase character");
    } else if (!number) {
      System.out.println("must contain at least one number");
    } else {
      break;
    }
  }
}
like image 113
cletus Avatar answered Oct 11 '22 15:10

cletus