Results 1 to 7 of 7
  1. #1
    davester is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Arrow while-loop stopping on first loop

    I have this code that is attempting to generate a code consisting of two 2 digit numbers (eg 56,27 ... 17,81 etc) between 10 and 99 that it sends to a validation class which returns if it is correct. I need the loop to continue attempting all possibilities until the connectionmanager class returns that it is correct, then it needs to stop.

    Here's what I have so far

    Java Code:
    public class openspaceship
    {
    public static void main(String[] args)
        {
        connectionmanager conn = new connectionmanager();
        combination comb = new combination();
            int i = 10;
            while (i <= 99)
            {
                int j = 10;
                while (j <= 99)
             {
                comb.setCombo(i, j);
           if (conn.openDoor(comb))
                {
                    System.out.println("Door opened, combination is " + i + j);
                }
                    j++;
             }
                    i++;
            }
        }
    }
    I tried putting a System.println above the set combo, and what it did was say 10,10 every time which makes me think that it is only looping once and not adding 1 to each value.

    The problem is that It will never find the code and always gets it wrong stopping after one loop.

    Can someone please amend this to function correctly. Or give some advice.

    Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Perhaps you could post a SSCCE?

    If you think i and j are not being incremented, this fact would be confirmed without needing the other classes.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Just a point of style, code like this:

    Java Code:
    int z = 10
    while (z <= 99) 
    {
        // stuff here...
    
        // and eventually
        z++;
    }
    is more readable if you keep the declaration, initialisation, update and checking of z all on one line. Which you can do with:

    Java Code:
    for (int z = 10; z < 100; z++)
    {
        // stuff here
    }

  4. #4
    davester is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default

    I commented out the IF statement, and then It generated the numbers as intended. So the problem is no longer in this class.

    Want me to pastebin all 5 classes?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Want me to pastebin all 5 classes?
    No!

    Well, that's just me... Others might be willing to track the problem down that way.

    The best way I know to track down a problem is to construct a really minimal example of it. That's why I posted the SSCCE link. They take work and stepping back from your code - but that's precisely why they're valuable, You end up questioning your assumptions.

    For instance if the problem in the code you originally posted was that the "Door opened" message was never printed although you expected it would be, that could be for all sorts of reasons - the two notable ones being that the combination was not one of the values you tried, or that the openDoor() method is not checking the combination as you intended.

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Another note of style... class names, by rigid convention, begin with uppercase letters. Also one thing we definitely need to see is the openDoor(comdination) method, as it may be doing something unexpected. (for example, if it is blocking, an infinite loop is possible). However, an SSCCE would be even better
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    stick in more print statements when your loops start. your only existing print statement is inside a condition, so if this is evaluating to false, you don't know whether or not your loops are executing for whatever reason. given that your loops iterate through ints, i highly doubt they're being terminated early.

Similar Threads

  1. while loop help
    By kathyla18 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 07:49 PM
  2. While loop
    By sjhentges in forum New To Java
    Replies: 11
    Last Post: 11-04-2008, 05:26 PM
  3. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  4. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:44 PM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM

Tags for this Thread

Posting Permissions

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