Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Password program for school

  1. #1
    Junior Member
    Join Date
    Jul 2024
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Password program for school

    Hey all, I am definitely new here and new to java.
    I have a program assignment for a intro to programming class that requires us to have a user enter a password and re prompt the entry until it is correct. I am not looking necessarily for the correct code but for pointers as to what exactly I am doing wrong. I suppose my issue is getting to the beginning of the "main" loop that counts characters while testing and then prompting the user for the right values later. I'll post what I have and if anyone has any pointers it would be much appreciated.

    /** A program to have a user enter a password with
     * specified requirements, and have the user re-enter
     * a password until those requirements are met.*/
     
    import java.util.Scanner; //allows user keyboard input
     
    public class ValidatePassword
    {
    	public static void main(String[] args) 
    	{
    	   String passwordRequirements ="Enter a password. Must have \n" //String describing password requiremts
    	   + "at least two uppercase letters, \n"
    	   + "at least three lowercase letters \n"
    	   + "and at least one digit.";
    	   String password;                         //String that holds the users password
    	   Scanner stdin = new Scanner(System.in);  //Scanner class that allows user input
    	   int passwordLength;                      //integer storing password length
     
    	   /*These next lines get the inital user password after defining the
    	   pass word requirements and then parrots back the password and stores
    	   and shows the length*/
     
    	   System.out.println(passwordRequirements);
    	   System.out.println("Enter your password: ");
    	   password = stdin.nextLine();
    	   passwordLength = password.length();
    	   System.out.println("The password you entered is " + password);   //These are for
    	   System.out.println("The password length is " + passwordLength);  //testing only
     
    	   int i;               //integer of position of character in the string
    	   char c;              //character used to determine upper/lower case
    	   int lowerCount = 0;  //integer of how many lower case characters there are
    	   int upperCount = 0;  //integer of how many upper case characters there are
    	   int digitCount = 0;
    	   final int MIN_LOWER = 3; //min number of lower case letters needed
    	   final int MIN_UPPER = 2; //min number of upper case letters needed
    	   final int MIN_DIG = 1;    //min number of numeric digits needed
     
    	   /*This section tests the password entered and has the user correct
    	   any wrong elements*/
     
    	   while (passwordLength < 6) //makes sure that password isn't too short
    	   {
    	        System.out.println("Your password is too short, please enter password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    	   }
     
     
    	   for(i = 0; i < passwordLength; i++)
    	   {
    		  c = password.charAt(i);
    		  if(Character.isUpperCase(c))
    		  {
    		      upperCount++;
    		  }
    		  else if(Character.isLowerCase(c))
    		  {
    		      lowerCount++;
    		  }
    		  else if(Character.isDigit(c))
    		  {
    		      digitCount++;
    		  }
     
    		}
     
    		  if(upperCount != MIN_UPPER)
    		  {
    		    while(upperCount < MIN_UPPER)
    		    {
    		    System.out.println("Password must contain at least 2 upper case letters!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    }
    		  }
     
    		  if(lowerCount != MIN_LOWER)
    		  {
    		    while(lowerCount < MIN_LOWER)
    		    {
    		    System.out.println("Password must contain at least 3 lower case letters!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    }
    		  }
     
    		  if(digitCount != MIN_DIG)
    		  {
    		    while(digitCount < MIN_DIG)
    		    {
    		    System.out.println("Password must contain at least 1 digit!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    }
    		  }
     
     
    		  System.out.println("The password you entered is " + password);
    	      System.out.println("Password length is " + passwordLength); 
    		  System.out.println("Upper Count: " + upperCount);
    		  System.out.println("Lower Count: " + lowerCount);
    		  System.out.println("Digit Count: " + digitCount);
     
     
    	}
    }
    Last edited by Norm; July 6th, 2024 at 05:56 AM. Reason: Added code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,115
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Password program for school

    what exactly I am doing wrong
    Can you explain why you think there is something wrong? What does the program do? What do you want it to do differently?
    Copy the contents of the command prompt window from when you execute the program and paste it here. Add some comments describing what is wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2024
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password program for school

    Here is what the command prompt outputs when I enter the password wrong. It will re prompt until the length is right, but for the other conditions, it will simply get stuck reprinting the requirement statement, never moving on even when the conditions are met. In this example, adding capital letters doesn't have the program move on to the next test. I feel like it needs to re run through the for loop that increments the character counters but there is no way to have it go back to a certain line, and repeating that for loop would have to be done infinitely if someone never puts in the correct conditions.

    Program run first with too short a password then with no capitals, then with all capitals it won't move out of that while loop.

    Enter a password. Must have
    at least two uppercase letters,
    at least three lowercase letters
    and at least one digit.
    Enter your password:
    1
    The password you entered is 1
    The password length is 1
    Your password is too short, please enter password:
    123456
    Password must contain at least 2 upper case letters!
    Please enter a new password:
    AA123456
    Password must contain at least 2 upper case letters!
    Please enter a new password:
    AAAAAA
    Password must contain at least 2 upper case letters!
    Please enter a new password:

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,115
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Password program for school

    What variable's value is controlling the while loop? Is that variable's value changed inside of the loop?
    If it is not changed the while loop will continue forever.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2024
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password program for school

    I feel kinda dumb, but that's how we learn I suppose, but can I use methods for length and upper/lower/digit counting. I've re written my code but all I am getting are illegal start of expressions errors. I'm guessing that somehow the variables can't be used but I'm confused as to how to make them useable by all methods. I feel like I am on the right track but the syntax technicalities are starting to get to me. Thank you all for any help. Code is here:

    /** A program to have a user enter a password with
     * specified requirements, and have the user re-enter
     * a password until those requirements are met.*/
     
    import java.util.Scanner; //allows keyboard input
     
    public class ValidatePassword
    {
    	public static void main(String[] args) 
    	{
    	   String passwordRequirements ="Enter a password. Must have \n" //String describing password requiremts
    	   + "at least two uppercase letters, \n"
    	   + "at least three lowercase letters \n"
    	   + "and at least one digit.";
    	   String password;                         //String that holds the users password
    	   Scanner stdin = new Scanner(System.in);  //Scanner class that allows user input
    	   int passwordLength;                      //integer storing password length
     
    	   /*These next lines get the inital user password after defining the
    	   password requirements and then parrots back the password and stores
    	   and shows the length*/
     
    	   System.out.println(passwordRequirements);
    	   System.out.println("Enter your password: ");
    	   password = stdin.nextLine();
    	   passwordLength = password.length();
    	   System.out.println("The password you entered is " + password);   //These are for
    	   System.out.println("The password length is " + passwordLength);  //testing only
     
    	   int i;               //integer of position of character in the string
    	   char c;              //character used to determine upper/lower case
    	   int lowerCount = 0;  //integer of how many lower case characters there are
    	   int upperCount = 0;  //integer of how many upper case characters there are
    	   int digitCount = 0;
    	   final int MIN_LOWER = 3; //min number of lower case letters needed
    	   final int MIN_UPPER = 2; //min number of upper case letters needed
    	   final int MIN_DIG = 1;    //min number of numeric digits needed
     
     
    	   /*This section tests the password entered and has the user correct
    	   any wrong elements*/
     
    	   public static void lengthTest()
    	   {
     
    	        while (passwordLength < 6) //makes sure that password isn't too short
    	        {
    	            System.out.println("Your password is too short, please enter password: ");
    		        password = stdin.nextLine();
    		        passwordLength = password.length();
    	        }
    	   }
     
    	   public static void counter()
    	   {
     
    	        for(i = 0; i < passwordLength; i++)
    	        {
    		        c = password.charAt(i);
    		        if(Character.isUpperCase(c))
    		        {
    		            upperCount++;
    		        }
    		         else if(Character.isLowerCase(c))
    		        {
    		            lowerCount++;
    		        }
    		        else if(Character.isDigit(c))
    		        {
    		            digitCount++;
    		        }
    		    }
    	   }
     
    		if(upperCount >= MIN_UPPER && lowerCount >= MIN_LOWER && digitCount >= MIN_DIG)
    		{
    		    System.out.println("Password accepted!");
    		}
     
    		else if(upperCount != MIN_UPPER)
    		  {
    		    while(upperCount < MIN_UPPER)
    		    {
    		    System.out.println("Password must contain at least 2 upper case letters!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    lengthTest();
    		    counter();
    		    }
    		  }
     
    		  else if(lowerCount != MIN_LOWER)
    		  {
    		    while(lowerCount < MIN_LOWER)
    		    {
    		    System.out.println("Password must contain at least 3 lower case letters!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    lengthTest();
    		    counter();
    		    }
    		  }
     
    		  else if(digitCount != MIN_DIG)
    		  {
    		    while(digitCount < MIN_DIG)
    		    {
    		    System.out.println("Password must contain at least 1 digit!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    lengthTest();
    		    counter();
    		    }
    		  }
    	}
    }
    Last edited by Norm; July 6th, 2024 at 01:06 PM. Reason: Added code tags

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,115
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Password program for school

    I am getting are illegal start of expressions errors
    Please copy the full text of the error message and paste it here.

    It looks like you have nested some methods inside of another method. That's not allowed.
    Make sure there are not any nested methods.

    Also Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2024
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password program for school

    Here is the error messages:

    ValidatePassword.java:93: error: cannot find symbol
    while (passwordLength < 6) //makes sure that password isn't too short
    ^
    symbol: variable passwordLength
    location: class ValidatePassword
    ValidatePassword.java:96: error: cannot find symbol
    password = stdin.nextLine();
    ^
    symbol: variable password
    location: class ValidatePassword
    ValidatePassword.java:96: error: cannot find symbol
    password = stdin.nextLine();
    ^
    symbol: variable stdin
    location: class ValidatePassword
    ValidatePassword.java:97: error: cannot find symbol
    passwordLength = password.length();
    ^
    symbol: variable passwordLength
    location: class ValidatePassword
    ValidatePassword.java:97: error: cannot find symbol
    passwordLength = password.length();
    ^
    symbol: variable password
    location: class ValidatePassword
    ValidatePassword.java:104: error: cannot find symbol
    for(i = 0; i < passwordLength; i++)
    ^
    symbol: variable i
    location: class ValidatePassword
    ValidatePassword.java:104: error: cannot find symbol
    for(i = 0; i < passwordLength; i++)
    ^
    symbol: variable i
    location: class ValidatePassword
    ValidatePassword.java:104: error: cannot find symbol
    for(i = 0; i < passwordLength; i++)
    ^
    symbol: variable passwordLength
    location: class ValidatePassword
    ValidatePassword.java:104: error: cannot find symbol
    for(i = 0; i < passwordLength; i++)
    ^
    symbol: variable i
    location: class ValidatePassword
    ValidatePassword.java:106: error: cannot find symbol
    c = password.charAt(i);
    ^
    symbol: variable c
    location: class ValidatePassword
    ValidatePassword.java:106: error: cannot find symbol
    c = password.charAt(i);
    ^
    symbol: variable i
    location: class ValidatePassword
    ValidatePassword.java:106: error: cannot find symbol
    c = password.charAt(i);
    ^
    symbol: variable password
    location: class ValidatePassword
    ValidatePassword.java:107: error: cannot find symbol
    if(Character.isUpperCase(c))
    ^
    symbol: variable c
    location: class ValidatePassword
    ValidatePassword.java:109: error: cannot find symbol
    upperCount++;
    ^
    symbol: variable upperCount
    location: class ValidatePassword
    ValidatePassword.java:111: error: cannot find symbol
    else if(Character.isLowerCase(c))
    ^
    symbol: variable c
    location: class ValidatePassword
    ValidatePassword.java:113: error: cannot find symbol
    lowerCount++;
    ^
    symbol: variable lowerCount
    location: class ValidatePassword
    ValidatePassword.java:115: error: cannot find symbol
    else if(Character.isDigit(c))
    ^
    symbol: variable c
    location: class ValidatePassword
    ValidatePassword.java:117: error: cannot find symbol
    digitCount++;
    ^
    symbol: variable digitCount
    location: class ValidatePassword
    18 errors



    And here is the code. There were nested methods, they have been moved, but the variables seem to be unable to be used outside of the main method.




     
    /** A program to have a user enter a password with
     * specified requirements, and have the user re-enter
     * a password until those requirements are met.*/
     
    import java.util.Scanner; //allows keyboard input
     
    public class ValidatePassword
    {
    	public static void main(String[] args) 
    	{
    	   String passwordRequirements ="Enter a password. Must have \n" //String describing password requiremts
    	   + "at least two uppercase letters, \n"
    	   + "at least three lowercase letters \n"
    	   + "and at least one digit.";
    	   String password;                         //String that holds the users password
    	   Scanner stdin = new Scanner(System.in);  //Scanner class that allows user input
    	   int passwordLength;                      //integer storing password length
     
    	   /*These next lines get the inital user password after defining the
    	   password requirements and then parrots back the password and stores
    	   and shows the length*/
     
    	   System.out.println(passwordRequirements);
    	   System.out.println("Enter your password: ");
    	   password = stdin.nextLine();
    	   passwordLength = password.length();
    	   System.out.println("The password you entered is " + password);   //These are for
    	   System.out.println("The password length is " + passwordLength);  //testing only
     
    	   int i;               //integer of position of character in the string
    	   char c;              //character used to determine upper/lower case
    	   int lowerCount = 0;  //integer of how many lower case characters there are
    	   int upperCount = 0;  //integer of how many upper case characters there are
    	   int digitCount = 0;
    	   final int MIN_LOWER = 3; //min number of lower case letters needed
    	   final int MIN_UPPER = 2; //min number of upper case letters needed
    	   final int MIN_DIG = 1;    //min number of numeric digits needed
     
     
    	   /*This section tests the password entered and has the user correct
    	   any wrong elements*/
     
     
    		if(upperCount >= MIN_UPPER && lowerCount >= MIN_LOWER && digitCount >= MIN_DIG)
    		{
    		    System.out.println("Password accepted!");
    		}
     
    		else if(upperCount != MIN_UPPER)
    		  {
    		    while(upperCount < MIN_UPPER)
    		    {
    		    System.out.println("Password must contain at least 2 upper case letters!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    lengthTest();
    		    counter();
    		    }
    		  }
     
    		  else if(lowerCount != MIN_LOWER)
    		  {
    		    while(lowerCount < MIN_LOWER)
    		    {
    		    System.out.println("Password must contain at least 3 lower case letters!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    lengthTest();
    		    counter();
    		    }
    		  }
     
    		  else if(digitCount != MIN_DIG)
    		  {
    		    while(digitCount < MIN_DIG)
    		    {
    		    System.out.println("Password must contain at least 1 digit!");
    		    System.out.println("Please enter a new password: ");
    		    password = stdin.nextLine();
    		    passwordLength = password.length();
    		    lengthTest();
    		    counter();
    		    }
    		  }
    	}
     
    	    public static void lengthTest()
    	    {    
     
    	        while (passwordLength < 6) //makes sure that password isn't too short
    	        {
    	            System.out.println("Your password is too short, please enter password: ");
    		        password = stdin.nextLine();
    		        passwordLength = password.length();
    	        }
    	    }
     
    	   public static void counter()
    	   {
     
    	        for(i = 0; i < passwordLength; i++)
    	        {
    		        c = password.charAt(i);
    		        if(Character.isUpperCase(c))
    		        {
    		            upperCount++;
    		        }
    		         else if(Character.isLowerCase(c))
    		        {
    		            lowerCount++;
    		        }
    		        else if(Character.isDigit(c))
    		        {
    		            digitCount++;
    		        }
    		    }
    	   }
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,115
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Password program for school

    variables seem to be unable to be used outside of the main method.
    Yes. Variables local to a method can not be seen by other methods.
    You need to think about building a class with variables and methods.
    One way to do that is to -
    move all the variables out of the main method into the class
    move the code from the main method into a constructor for the class
    have the main method create an instance of the class -> that will call its constructor which can do the job.

    Learning to write programs using classes and methods will be what you'll want to do to make progress learning java.

    Putting all the code in a main method is a common beginner mistake.

    Just to be honest there is a way to continue using the main method as you are doing -
    make everything static - that means it is not necessary to create an instance of the class. It is a quick and dirty way to make a simple program but is not an OOP technique.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2024
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password program for school

    Norm, thank you for the help. I suppose I still have a lot to learn in terms of constructors and what can and cannot be in or out of a class and where it can be used. I did get it working by making them all static variables and separate method calls. It seems to be quirky in how it counts digits and where that takes it but it is overall working in terms of prompting until the password is useable. Here is the updated code.

     
    /** A program to have a user enter a password with
     * specified requirements, and have the user re-enter
     * a password until those requirements are met.*/
     
    import java.util.Scanner;		//allows keyboard input
     
    public class ValidatePassword
    {
      static String password;		//String that holds the users password
      static Scanner stdin = new Scanner (System.in);	//Scanner class that allows user input
      static int passwordLength;	//integer storing password length
     
      static int i;					//integer of position of character in the string
      static char c;				//character used to determine upper/lower case
      static int lowerCount = 0;	//integer of how many lower case characters
      static int upperCount = 0;	//integer of how many upper case characters
      static int digitCount = 0;    //integer of how many digits
      static final int MIN_LOWER = 3;	//min number of lower case letters needed
      static final int MIN_UPPER = 2;	//min number of upper case letters needed
      static final int MIN_DIG = 1;	//min number of numeric digits needed
     
     
      public static void main (String[]args)
      {
    	String passwordRequirements = "Enter a password. Must have \n"	//String describing password requiremts
    	  + "at least two uppercase letters, \n"
    	  + "at least three lowercase letters \n" + "and at least one digit.";
     
    	/*These next lines get the inital user password after displaying
    	   password requirements*/
     
    	  System.out.println (passwordRequirements);
    	  System.out.println ("Enter your password: ");
    	  password = stdin.nextLine ();
    	  passwordLength = password.length ();
     
    	/*This section tests the password entered and has the user correct
    	 any wrong elements */
     
     
    	  lengthTest ();
    	  counter ();
    	  upperLetters ();
    	  lowerLetters ();
    	  digits ();
     
    	  System.out.println("Digits are: " + digitCount);
      }
     
      public static void lengthTest () //makes sure that password isn't too short
      {
    	while (passwordLength < 6)	
    	  {
    		System.out.println ("Your password is too short, please enter a new password: ");
    		password = stdin.nextLine ();
    		passwordLength = password.length ();
    	  }
      }
     
      public static void counter () //counts uppercase, lowercase and digits
      {
     
    	for (i = 0; i < passwordLength; i++)
    	  {
    		c = password.charAt (i);
    		if (Character.isUpperCase (c))
    		  {
    			upperCount++;
    		  }
    		else if (Character.isLowerCase (c))
    		  {
    			lowerCount++;
    		  }
    		else if (Character.isDigit (c))
    		  {
    			digitCount++;
    		  }
    	  }
     
    	    if (upperCount >= MIN_UPPER && lowerCount >= MIN_LOWER
    		    && digitCount >= MIN_DIG)
    	    {
    		    System.out.println ("Password accepted!");
    	    }
      }
     
      public static void upperLetters () //ensures the correct amount of uppercase letters.
      {
    	while (upperCount < MIN_UPPER)
    	  {
    		System.out.println ("Password must contain at least 2 upper case letters!");
    		System.out.println ("Please enter a new password: ");
    		password = stdin.nextLine ();
    		passwordLength = password.length ();
    		lengthTest ();
    		counter ();
    	  }
      }
     
      public static void lowerLetters () //ensure the right amount of lowercase letters
      {
    	while (lowerCount < MIN_LOWER)
    	  {
    		System.out.println ("Password must contain at least 3 lower case letters!");
    		System.out.println ("Please enter a new password: ");
    		password = stdin.nextLine ();
    		passwordLength = password.length ();
    		lengthTest ();
    		counter ();
    	  }
      }
     
      public static void digits ()
      {
    	while (digitCount < MIN_DIG)
    	  {
    		System.out.println ("Password must contain at least 1 digit!");
    		System.out.println ("Please enter a new password: ");
    		password = stdin.nextLine ();
    		passwordLength = password.length ();
    		lengthTest ();
    		counter ();
    	  }
      }
     
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,115
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Password program for school

    seems to be quirky in how it counts digits
    Please explain what you see and why you think it is quirky.

    it is overall working
    I don't think it is.
    Try these inputs:
    AAAAAA
    bbbbbb
    111111
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jul 2024
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password program for school

    It's quirky in that it seems never to recount anything but the length test. As long as at some point the conditions were met, even if the current password entered is wrong, it will accept it and say it is valid even though it is not. Those three inputs you listed are one of a few examples I was able to do, where it will call it a valid password even though it is not.

  12. #12
    Junior Member
    Join Date
    Jul 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Password program for school

    For checking password if it has required number of uppercase, lowercase,special characters, numbers etc. you can go for regular expression, which will check if your entered password follows the required constraints.

    Second you want to check if your re-entered password is same as the first one and until it doesn't match , you will be prompted to re-enter the password. For this I am assuming you dont have any fixed number of retries constraint, So you can go for while or do-while loop. Inside the loop you can check if the first and second password(i.e re-entered password) is same or not. If same you can break from the loop or else give a prompt that the re-entered password is wrong and ask to enter the password again.

    Hope this helps and All the Best!!!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,115
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Password program for school

    it will call it a valid password even though it is not.
    Yes, the code needs to be rewritten. Start by making a list of steps the code needs to do.
    It needs a main loop. Inside the loop it should prompt the user, get a new password and then make the tests. If a test fails write an error message and go back to the top of the loop. If all tests succeed then exit the loop.

    Some comments on looping:
    here are three ways to proceed inside the loop
    1) go to the next step - normal
    2) go to the top of the loop with the continue statement
    3) exit the loop with the break statement.

    Another way to exit a while loop is to change the value of the variable that is controlling the looping.

    The methods that do the tests for validity should return true if the test passed and false if not.
    For example:
      //  Test password is long enough
      private boolean lengthOK(String pswd) {
          return pswd.length() >= MIN_LENGTH;
      }

    Then use it inside of a loop:
       if(!lengthOK(password)) {
          // write an error message about the length
          continue;     // go back and get another password
       }

    Some of the variables that are global scope should be moved inside of the methods where they are used.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Username/Password Different Classes Java program help
    By JAVAHELPP in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 18th, 2013, 04:57 PM
  2. Password Generating Program Help
    By iAce in forum Java Theory & Questions
    Replies: 7
    Last Post: December 22nd, 2012, 09:00 PM
  3. Can Someone please help me with my password program?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2012, 11:16 AM
  4. Check Password Program
    By m2msucks in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 6th, 2011, 01:39 AM
  5. Password Program
    By computercoder in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 20th, 2010, 07:03 AM