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

Thread: learning while and for loops

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default learning while and for loops

    Hello!

    My name is Jon and I am taking my first programming class in Java. I have to write a program that asks the user to enter a "new password." The program should check that the user's password has at least one uppercase letter, one lowercase letter, and one non-letter character. Furthermore, if the user enters an incorrect password, the program should keep asking for a new password until it gets a valid one. if the use enters an incorrect password 3 times, the program stops asking the user for a new password.

    I'm sure there are 999 different ways to write this code. I'm really close with what I have, so I'm hoping I don't have to change everything around entirely. my code can correctly test whether the user's password is valid or not. i'm having trouble getting it to ask the user for a new pw if it is not valid the first time. Anyway, here's what I have so far!
    import java.util.*;
     
    public class HWHelp
    {
    	public static void main (String [] args)
    	{
    		boolean hasUpper = false;
    		boolean hasLower = false;
    		boolean hasNonLetter = false;
    		boolean passwordSet = false;
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Please enter a new password. Make sure your password "
    			+ "contains at least one uppercase letter, one lowercase letter, "
    			+ "and at least one character that is either a numerical digit or other "
    			+ "non-letter.");
     
    		String usersPw = scan.next();
    		int j = 0;
     
    		while (!passwordSet && (j < 3))
    		{
    			for (int i = 0; i < usersPw.length(); i++)
    			{	
    				while (i < usersPw.length())
    				{
    					char character = usersPw.charAt(i);
    					if (character >= 65 && character <= 90)
    					{
    						hasUpper = true;
    					}
    					if (character >= 97 && character <= 122)
    					{
    						hasLower = true;
    					}
    					if (character < 65 || (character > 90 && character < 97) || character > 122)
    					{
    						hasNonLetter = true;
    					}
    					i++;
    				}			
    			}
    			if (hasUpper && hasLower && hasNonLetter)
    			{
    				passwordSet = true;
    			}
    			else
    			{
    				passwordSet = false;
    			}
    			j++;
    		}
    		if (passwordSet)
    		{
    			System.out.println("Password set.");
    		}
    		else
    		{
    			System.out.println("Password change aborted.");
    		}
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: learning while and for loops

    You have a loop that keeps going if the password is not set and is less than 3 attempts, so it probably makes sense to ask the user to enter the new password inside the loop. What do you think?
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    jonnyg100 (November 9th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: learning while and for loops

    Thank you! I figured it out! That was a big help.

    JG

Similar Threads

  1. Learning together
    By Melawe in forum The Cafe
    Replies: 34
    Last Post: November 10th, 2011, 04:58 AM
  2. How to start learning JNI
    By 1bun100 in forum Java Native Interface
    Replies: 1
    Last Post: September 20th, 2011, 09:38 AM
  3. Learning Java
    By destruxion in forum Member Introductions
    Replies: 1
    Last Post: September 12th, 2011, 04:13 AM
  4. learning group
    By cooler4hell in forum The Cafe
    Replies: 6
    Last Post: January 27th, 2011, 05:02 AM
  5. :!! Unsure how to set this up/ Still learning java loops!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 4
    Last Post: September 29th, 2009, 11:28 AM