Results 1 to 7 of 7
- 06-26-2009, 10:05 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 2
- Rep Power
- 0
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
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.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++; } } }
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
- 06-26-2009, 10:40 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
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.
- 06-26-2009, 10:43 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
Just a point of style, code like this:
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:int z = 10 while (z <= 99) { // stuff here... // and eventually z++; }
Java Code:for (int z = 10; z < 100; z++) { // stuff here }
- 06-26-2009, 10:48 AM #4
Member
- Join Date
- Jun 2009
- Posts
- 2
- Rep Power
- 0
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?
- 06-26-2009, 11:04 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
No!Want me to pastebin all 5 classes?
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.
- 06-26-2009, 07:26 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 06-26-2009, 08:46 PM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
-
while loop help
By kathyla18 in forum New To JavaReplies: 1Last Post: 03-02-2009, 06:49 PM -
While loop
By sjhentges in forum New To JavaReplies: 11Last Post: 11-04-2008, 04:26 PM -
How to use Do While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
How to use While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:44 PM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks