Results 1 to 3 of 3
  1. #1
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default Help with While and For Looping

    I am new to Java and trying to learn it as quick as possible. I am taking a course and I am having trouble with while and for loops. I wrote this code and I am having some trouble. Can anyone help me with it? Here is what I have so far.

    import javax.swing.JOptionPane;

    public class Lab4
    {
    public static void main(String[] args)
    {
    //WARNING!!! This code's logic is INCORRECT.
    System.out.println("While loop");
    String uname = JOptionPane.showInputDialog("What is your username?");
    String pword = JOptionPane.showInputDialog("What is your password?");
    int trials = 3;
    while( !(uname.equals("coffee") && pword.equals("JavaLover")) || trials < 3)
    {
    System.out.println(trials);
    JOptionPane.showMessageDialog(null, "Invalid combination, try again");
    uname = JOptionPane.showInputDialog("What is your username?");
    pword = JOptionPane.showInputDialog("What is your password?");
    trials++;
    }
    }
    }

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    You want to change the OR || to an AND && becuase you want to ask them again if they A: typed the wrong user/pass AND if B: they haven't had 3 trials
    Also, you want to change the start value of trials to 1 Because when they have gotten to the while loop, they have already had one try.

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    im also very new to java so not sure if this will help much or even if its absolutley correct

    but looking at
    Java Code:
    while( !(uname.equals("coffee") && pword.equals("JavaLover")) || trials < 3)
    this boolean will always be true IF trials is less than three.

    if you had
    Java Code:
    while( !(uname.equals("coffee") || pword.equals("JavaLover")) && trials < 3)
    this will only be true if uname OR password are incorect and trials is less than three.

    i think!:)

    you dont really say what the problem is specifically or what it is doing wrong
    hope this helps,
    if not i'm sure someone who knows for certain will be able to help out
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

Similar Threads

  1. looping and filtering
    By javafanatic in forum New To Java
    Replies: 14
    Last Post: 02-09-2010, 09:48 AM
  2. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM
  3. looping problems
    By Blakester in forum New To Java
    Replies: 4
    Last Post: 10-05-2009, 08:26 PM
  4. how to do conditional looping?
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 12:38 PM
  5. Looping problem
    By Tanilo in forum New To Java
    Replies: 1
    Last Post: 08-01-2008, 06:34 AM

Posting Permissions

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