aspose file tools*
The moose likes Beginning Java and the fly likes Why wont these nested loops run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why wont these nested loops run?" Watch "Why wont these nested loops run?" New topic
Author

Why wont these nested loops run?

Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
Im creating a program that asks the user for a 6 digit number consisting of dice face values, ex: 123456. A die will roll until the pattern 123456 has been rolled in that exact format, but for some reason, my third while loop in the portion of code ive shown wont execute. any ideas? I can show you the entire code if you need it
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
The only way to get out of the second loop is for gotFirst to not be 1. For this to happen line 25 has to be executed and if line 25 is executed, line 26 will be executed as well. So when you come out of the second loop, gotSecond will be 0 and you only enter the third loop if gotSecond is 1.
Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
But i need gotFirst to be 1, the 1's mean that the die rolled onto the first number out of six. so if the die doesnt land on the correct face, it will be given the value 0 and try to get the first number again, starting the process all over.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

I don't understand the rules you're trying to implement there. Okay, I understand the first loop where you roll the dice until you match the first number. But why isn't the second loop the same, so that you roll the dice until you match the second number? Isn't that the rule? If not, then what is the rule?

I have to say that I don't even understand what this means:

A die will roll until the pattern 123456 has been rolled in that exact format


In particular I don't understand what "in that exact format" means.
Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
Sorry, let me try to be more specific. If i input that i want 123456 as my 6 digit number, the die will roll until i get a 1. Then on the next roll, if it lands on anything but a 2, everything resets and it has to land on 1 again before going to 2. If the die does land on 2 after a 1, then the next roll must be a 3 or the process resets until the 6th number is reached. But with the code i have now, it never reads the 3 after landing a 1 and 2.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Ah, okay. So if in your second loop you don't roll the second number right away on the first try, then you have to go back and redo the first loop again, right? Your code isn't doing that.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40


Also, for a topic named "Why wont these nested loops run?", it must be pointed out that there isn't a nested loop in the code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
Henry Wong wrote:
Also, for a topic named "Why wont these nested loops run?", it must be pointed out that there isn't a nested loop in the code.

Henry

oops
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Here's a hint: For the second and subsequent rolls you don't need a loop. That's because you can only try each roll once.
Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
Well i got to it finally check for the entire pattern. But im having trouble getting the program to reset and try to roll for the first number whenever a number doesnt match. Right now, if i want to get 123456 and get a 1 on a roll, itll see if the next 5 numbers match 23456 before resetting. I want it to reset if i roll a 1 and dont get a 2 after.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Hoang wrote:Well i got to it finally check for the entire pattern. But im having trouble getting the program to reset and try to roll for the first number whenever a number doesnt match. Right now, if i want to get 123456 and get a 1 on a roll, itll see if the next 5 numbers match 23456 before resetting. I want it to reset if i roll a 1 and dont get a 2 after.


Then you have a bug in your code. You need to fix that bug if you want it to behave properly.
Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
Yeah thats the problem, i cant seem to figure it out, im not sure if it still does it since i took out the System.out.printlns that showed me how my program was running, but heres my code now.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Look at this:



0 means you haven't got it yet, and 1 means you got it, right? (And if so, don't use ints--use booleans).

So that says "keep going as long as..." what? Describe the condition under which that loop will keep going, based on that line of code. Describe it in English, in terms of the rules of your game, not in terms of java or loops.

Aside from that, and from using ints where you should be using booleans, that code is rather painful to read. The logic is really hard to follow. Too much duplicated code, too much nesting. It would be clearer if you used methods (for less duplicated code and less nesting) and arrays (for less duplicated code).
Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
yes 0 means i havent got the number, 1 means i have. The condition i tried using for that loop is, keep rolling the die if none of the numbers in pattern have been matched. The while condition you quoted may be unneccessary, but i wanted to make sure the game would keep going until the six digit number has been matched. Maybe i could shorten it by making it } while ((gotSixth ==0)); since gotSixth ==1 can only be achieved if all the numbers before it have been matched. Also i know its hard to read, im not allowed to use methods or arrays. This is one game of 6, so i can only have a test and 6 classes for each game.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
What dreadful long lines. I have shown you the correct way to divide them, so you can actually read the code.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Daniel Hoang wrote:Also i know its hard to read, im not allowed to use methods or arrays...

Hunh? . Must be some new kind of teaching by torture...

All I can say is: once you've finished this exercise, forget it ever existed.

You always want to use methods to break up logic - and, within reason: the more, the better.

Also, that has virtually nothing to do with making your code neat and easy to read.

As for arrays: there are other alternatives, which you'll probably learn about soon; but for a fixed-sized group of values, they are exactly what you should use - and don't let anyone tell you any different.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Hoang wrote:yes 0 means i havent got the number, 1 means i have. The condition i tried using for that loop is, keep rolling the die if none of the numbers in pattern have been matched.


You're contradicting yourself. Earlier you said that as soon as any one digit doesn't match, you have to start over--in other words, if the desired number is 1234, then you have to roll a 1, and the very next roll has to be a 2, and the very next roll after that has to be a 3, and then the very next roll has to be a 4.

What you're saying here, and what that do/while condition says, is keep rolling only if all the numbers are wrong. So if I got a 1 and then a 5, since the 1 matched, the "none have been matched" condition is false, so we break out of the loop.

And if you're really not allowed to use methods, you should drop the class, because the teacher is incompetent. This could be made much easier to understand with proper division of responsibility.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why wont these nested loops run?