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 4 of 4

Thread: Need help

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help

    I am currently trying to create a simple password program that contains at least 1 letter and 1 digit, and requires the user to enter the password a second time to accept it. Below is my code, any help would be greatly appreciated. Currently my program asks for a password and then just repeats it over and over with no end to the loop.
    import java.util.Scanner;
     
    public class Password2 
    {
      public static void main(String[] args) 
       {
        Scanner input = new Scanner(System.in);
     
        System.out.println("Welcome please enter your password.>>");
    	 enterPassword();
        String password = "";
        String enterPassword = "";
        //keep going until we get an acceptable password
        while(!CheckPassword(password))
        {     
     
    		password =(enterPassword);
        }
        System.out.println("Password accepted");
        }
     
     
        public static Boolean CheckPassword(String password)
        {
        //perform all password checks
        Boolean passedLength = checkPasswordLength(password);
        Boolean passedLetter = checkPasswordLetter(password);
        Boolean passedDigit = checkPasswordDigit(password);
        return (passedLength && passedLetter && passedDigit);                 
     
        }
     
    public static String enterPassword(){
        String password;
        Scanner input = new Scanner(System.in);
        System.out.print("Password >>");
        password = input.nextLine();
     
    	 return password;	
     
        }
     
     
    public static Boolean checkPasswordLength(String password)
        {
        //passes if there is a string value, and it has 6+ characters
        int length;
            length = password.length();
            while (length <6){
                enterPassword();
                }
    		  {
            checkPasswordLetter(password);
            }
    		return true;
        }
     
    public static Boolean checkPasswordLetter(String password)
        {
    	   int let = 1;
     
     
    		while (let <1)
    		{
    		 enterPassword();
    		}
    		{
    		checkPasswordDigit(password);
    		}
    		return true;
        }
    public static Boolean checkPasswordDigit(String password)
        {
    	   int digit = 1;
    		while (digit >=1)
    		{
    		  System.out.println("Password accepted");
    		}
     
    		{
    		enterPassword();
    		}
    		return true;
        }
    }

  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Need help

    Hello mikelc.

    Welcome to the Java Programming Forums.

    I would use System.out.println(); to see exactly what is going on.

    After doing this, we can discover that there are several issues when passing information between methods.

    For example:

    	public static Boolean CheckPassword(String password) {
    		// perform all password checks
    		System.out.println("CheckPassword " + password);
    		Boolean passedLength = checkPasswordLength(password);
    		System.out.println(passedLength);
    		Boolean passedLetter = checkPasswordLetter(password);
    		System.out.println(passedLetter);
    		Boolean passedDigit = checkPasswordDigit(password);
    		System.out.println(passedDigit);
    		return (passedLength && passedLetter && passedDigit);
     
    	}

    This shows us that this method never receives the correct password value.

    You can fix it like this:

    import java.util.Scanner;
     
    public class Password2 {
     
    	public static String password;
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Welcome please enter your password.>>");
    		enterPassword();
    		//String password = "";
    		String enterPassword = "";
    		// keep going until we get an acceptable password
    		while (!CheckPassword(password)) {
     
    			password = (enterPassword);
    		}
    		System.out.println("Password accepted");
    	}
     
    	public static Boolean CheckPassword(String password) {
    		// perform all password checks
    		System.out.println("CheckPassword " + password);
    		Boolean passedLength = checkPasswordLength(password);
    		System.out.println(passedLength);
    		Boolean passedLetter = checkPasswordLetter(password);
    		System.out.println(passedLetter);
    		Boolean passedDigit = checkPasswordDigit(password);
    		System.out.println(passedDigit);
    		return (passedLength && passedLetter && passedDigit);
     
    	}
     
    	public static String enterPassword() {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Password >>");
    		password = input.nextLine();
    		System.out.println("enterPassword - " + password);
    		return password;
     
    	}
     
    	public static Boolean checkPasswordLength(String password) {
    		// passes if there is a string value, and it has 6+ characters
    		int length;
    		length = password.length();
    		while (length < 6) {
    			enterPassword();
    		}
    		{
    			checkPasswordLetter(password);
    		}
    		return true;
    	}
     
    	public static Boolean checkPasswordLetter(String password) {
    		int let = 1;
     
    		while (let < 1) {
    			enterPassword();
    		}
    		{
    			checkPasswordDigit(password);
    		}
    		return true;
    	}
     
    	public static Boolean checkPasswordDigit(String password) {
    		int digit = 1;
    		while (digit >= 1) {
    			System.out.println("Password accepted");
    		}
     
    		{
    			enterPassword();
    		}
    		return true;
    	}
    }

    There are still issues to address though.. I think this will be enough to help you move forward.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help

    Thanks for the help, but after trying this code out it throws the password accepted statement into an infinite loop to say. It keeps repeating over and over, with no end. But thanks for the help.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help

    I have refined my code but now i need help on trying to get the program to prompt the user to re-enter the password when it does not meet the specified criteria. Also after i enter a valid password it prompts me to re-enter the it adds another entry line and if i either press the enter key or enter the same password again it then accepts it. Could some one help. Here is my code:

    import java.util.Scanner;

    public class Password2
    {
    public static void main(String[] args)
    {
    char aChar;
    int length;
    String aString;
    String password;
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome please enter your password.>>");
    aString = input.nextLine();
    aChar = aString.charAt(0);
    length = aString.length();
    Character.isLetter(aChar);
    Character.isDigit(aChar);
    Scanner input1 = new Scanner(System.in);
    System.out.println("Re-enter to confirm Password");
    password = input1.nextLine();
    if(length <6 || length >10 && aChar <1)

    System.out.println("Password must contain at least 6-10 characters.");

    if(password.equals(aString))
    {
    System.out.println("Password accepted.");
    }
    }


    }