views:

241

answers:

3

Hello, I am trying to make a program in Java that checks for three specific inputs. It has to be 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)
                {







                }

            }
+2  A: 

A regular expression is more appropriate for what you are trying to accomplish. For instance, using lookahead assertions, you'd use something like this:

Pattern p = Pattern.compile("^.*(?=.{7,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$");
String pass = getStrSys();
Matcher m = p.matcher(pass);
if (m.matches()) {
  System.out.println("Valid password.");
} else {
  System.out.println("Invalid password.");
} 
JG
This is an example of being too clever for one's own good. Not only in the expression almost unreadable but if it doesn't pass it doesn't tell you why and you need to tell the user that, so it's basically useless.
cletus
Agreed with Cletus. This is a possible approach and it's a valid answer to the question, but it's a maintenance nightmare. It takes a lot of cognitive overhead to look at that regex and understand what causes it to pass.
John Feminella
I think it's perfectly readable if one understands the lookahead operator. As for not telling the user which restriction is failing, I'm sure when you are validating an e-mail, you don't tell them if it's an invalid domain, or if a dot is missing: you just say the user has entered an invalid email; same goes with password validation.
JG
@JG Uh... no. You get a fail on that. Tell someone to enter an email address and they know what to enter. Tell someone to enter a password and you have to tell them *why* it's invalid. It's like saying "try again". Even if you state all the password rules, someone might not realize what they entered didn't match or they may have simply mistyped something.
cletus
Then everyone is getting it wrong, Google, Yahoo, et cetera, because they all state the rules, and then they just say "try again" if the password doesn't comply. But go ahead, I guess it's okay to downvote a perfectly valid answer for a typical homework question these days.
JG
FWIW this is also 20x slower than my version (I ran a test as I was curious).
cletus
+1  A: 
  1. Don't use StringTokenizer. Use String.split.
  2. Use the functions in the Character class to check for upper case, lower case, or numeric.
  3. Since you've forced lower case, you can't check. You need to get rid of that.
bmargulies
+4  A: 
cletus