Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-26-2009, 11:05 AM
Member
 
Join Date: Jun 2009
Posts: 2
Rep Power: 0
davester is on a distinguished road
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

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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-26-2009, 11:40 AM
Senior Member
 
Join Date: Feb 2009
Posts: 632
Rep Power: 2
pbrockway2 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-26-2009, 11:43 AM
Senior Member
 
Join Date: Feb 2009
Posts: 632
Rep Power: 2
pbrockway2 is on a distinguished road
Default
Just a point of style, code like this:

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:

Code:
for (int z = 10; z < 100; z++)
{
    // stuff here
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-26-2009, 11:48 AM
Member
 
Join Date: Jun 2009
Posts: 2
Rep Power: 0
davester is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-26-2009, 12:04 PM
Senior Member
 
Join Date: Feb 2009
Posts: 632
Rep Power: 2
pbrockway2 is on a distinguished road
Default
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-26-2009, 08:26 PM
Senior Member
 
Join Date: Mar 2009
Posts: 392
Rep Power: 2
Singing Boyo is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-26-2009, 09:46 PM
Senior Member
 
Join Date: Sep 2008
Posts: 564
Rep Power: 2
emceenugget is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
generate, password

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 06:28 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org