Results 1 to 4 of 4
  1. #1
    twcast is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Need help with getting second loop to work

    hi everyone, I have been working with java for a couple weeks, and there is so much I do not understand. I have a simple Password Checker code that works OK until I try to get the second password input to do a loop.

    (If I remove most of the code after "while (var)" from the second password field)

    I am trying to get the code to keep looping until the passwords match
    import javax.swing.JOptionPane;

    public class PasswordChecker
    {
    public static void main(String[] args)
    {


    String firstPassword =" ";
    String secondPassword = " ";
    boolean var = true;

    while(var)
    {
    firstPassword = JOptionPane.showInputDialog("Enter new password");

    int len = firstPassword.length();

    if (len < 6 || len > 10)
    {
    JOptionPane.showMessageDialog(null, "More than six and less than 10 characters are needed");
    }
    else
    {

    int count1 = 0, count2 = 0;
    for(int i = 0; i < len; i++)
    {
    if(Character.isDigit(firstPassword.charAt(i)))
    {
    count1++;
    }
    else if(Character.isLetter(firstPassword.charAt(i)))
    {
    count2++;
    }
    }

    if(count1 > 0 && count2 > 0)
    {
    var = false;
    }
    else

    {
    JOptionPane.showMessageDialog(null, "Please put at least a letter and a number");
    }
    }
    }
    while (var)
    {
    secondPassword = JOptionPane.showInputDialog("Re-enter your Password");


    if (secondPassword.equals(firstPassword))
    {
    JOptionPane.showMessageDialog(null, "You entered the correct password", "OK", JOptionPane.PLAIN_MESSAGE);

    }

    else
    JOptionPane.showMessageDialog(null, "You May Not Enter");
    int len = secondPassword.length();

    if (len < 6 || len > 10)
    {
    JOptionPane.showMessageDialog(null, "More than six and less than 10 characters are needed");
    }
    else
    {

    int count1 = 0, count2 = 0;
    for(int i = 0; i < len; i++)
    {
    if(Character.isDigit(secondPassword.charAt(i)))
    {
    count1++;
    }
    else if(Character.isLetter(secondPassword.charAt(i)))
    {
    count2++;
    }
    }

    if(count1 > 0 && count2 > 0)
    {
    var = false;
    }
    else

    {
    JOptionPane.showMessageDialog(null, "Please put at least a letter and a number");
    }
    }
    }




    System.exit(0);

    }
    }


  2. #2
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Underlined are the changes:
    Java Code:
    import javax.swing.JOptionPane;
    
    public class PasswordChecker
    {
        public static void main(String[] args)
        {
            String firstPassword =" ";
            String secondPassword = " ";
            boolean var = true;
            while(var)
            {
                firstPassword = JOptionPane.showInputDialog("Enter new password");
                int len = firstPassword.length();
                if (len < 6 || len > 10)
                {
                    JOptionPane.showMessageDialog(null, "More than six and less than 10 characters are needed");
                }
                else
                {
                    int count1 = 0, count2 = 0;
                    for(int i = 0; i < len; i++)
                    {
                        if(Character.isDigit(firstPassword.charAt(i)))
                        {
                            count1++;
                        }
                        else if(Character.isLetter(firstPassword.charAt(i)))
                        {
                            count2++;
                        }
                    }
                    if(count1 > 0 && count2 > 0)
                    {
                        var = false;
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "Please put at least a letter and a number");
                    }
                }
            }
            [U]secondPassword = JOptionPane.showInputDialog("Re-enter your Password");
            if (secondPassword.equals(firstPassword))
            {
                JOptionPane.showMessageDialog(null, "You entered the correct password", "OK", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "You May Not Enter");
            }[/U]
            System.exit(0);
        }
    }
    It seems unreasonable to ask the user the second time to enter at least a letter and a number and/or enter more than 6 and less than 10 characters because the second password HAS to be the first password, else the second password is incorrect and the user may not enter. Although it works, it doesn't solve your problem right?
    EDIT: Actually try this:
    Java Code:
    ...
    
    String firstPassword =" ";
    String secondPassword = " ";
    boolean var = true;
    [U]boolean var2 = true;[/U]
    
    ...
    
    [U]while (var2)[/U]
    {
    secondPassword = JOptionPane.showInputDialog("Re-enter your Password");
    
    
    if (secondPassword.equals(firstPassword))
    {
    JOptionPane.showMessageDialog(null, "You entered the correct password", "OK", JOptionPane.PLAIN_MESSAGE);
    
    }
    
    else
    {
        JOptionPane.showMessageDialog(null, "You May Not Enter");
    }
    int len = secondPassword.length();
    
    if (len < 6 || len > 10)
    {
    JOptionPane.showMessageDialog(null, "More than six and less than 10 characters are needed");
    }
    else
    {
    
    int count1 = 0, count2 = 0;
    for(int i = 0; i < len; i++)
    {
    if(Character.isDigit(secondPassword.charAt(i)))
    {
    count1++;
    }
    else if(Character.isLetter(secondPassword.charAt(i)))
    {
    count2++;
    }
    }
    
    if(count1 > 0 && count2 > 0)
    {
    [U]var2 = false;[/U]
    }
    else
    
    {
    JOptionPane.showMessageDialog(null, "Please put at least a letter and a number");
    }
    }
    }
    
    
    
    
    System.exit(0);
    
    }
    }
    The output is "You May Not Enter" and then "Enter at least a letter and a number" and/or "Enter more than 6 and less than 10 characters" when the incorrect input is entered, so you might need to fix things around a bit.
    EDIT2: The problem here was that after the first loop, the boolean became false, so then the second loop will not execute; so you can as well add "var = true;" after the first loop in your original code
    Last edited by Rhez; 02-02-2010 at 03:36 AM. Reason: found solution to problem

  3. #3
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Check out the control of flow tutorial: Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Particularly the java language key words: break, continue.

  4. #4
    twcast is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. do-while loop doest work here? help me
    By dhanh90 in forum New To Java
    Replies: 4
    Last Post: 10-18-2009, 02:57 AM
  2. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  3. Why won't this while loop work?
    By trueblue in forum New To Java
    Replies: 6
    Last Post: 05-23-2009, 08:10 PM
  4. my loop doesn't work.. pls help???
    By ashton in forum New To Java
    Replies: 5
    Last Post: 01-16-2009, 08:24 AM
  5. Why doesn't my loop work?
    By d0nmin0 in forum Advanced Java
    Replies: 8
    Last Post: 05-26-2008, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •