jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Do-while loop index problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Do-while loop index problem" Watch "Do-while loop index problem" New topic
Author

Do-while loop index problem

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Hi all:

I am currently working on a program that requires the use of a do-while loop. I am having some difficulty with determining what the while() conditions are going to be though. To keep things simple, I have created some very simple code.



What I want the program to do is, in this simple case, starting printing strings beginning at items[3] (which is where the variable startAt is set and is used), and I need it to print not only the last element, items[9], but to start at the beginning and keep going BUT DO NOT print the element we started at, which is items[3].

So in other words, what it should look like is this by the time it's done...
four, five, six, seven, eight, nine, ten, one, two, three

Obviously with no commas of course, but still it should keep printing and then STOP before it reaches items[3], or the string "four".

Can anyone tell me what the exact conditions are that need to be put into the while part?
I know for some of you, you may think it may be more efficient to use a for loop perhaps, but there is a reason I'm using a do-while.

Thanks in advance everyone.

S.T.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Why do you think it's that simple?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
The code I posted is a much simpler version than what I really have.

The rest of the code in the actual program is fine, it's just that I don't know exactly how to tell where to stop in the do-while loop.

Basically that is where the problem lies I found.

Once someone is able to indicate to me how it should be written or expressed in the while part, then the method I have that has the do-while loop willl be ready to go.

S.T.
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

Sam,

Walk away from your computer and use paper and a pencil. Now how (in english) how would you solve that problem?

After doing so, you might find a couple of ways of doing this.....

like as soon as in the loop starts increment the start at, then test for that value coming around again....

Or, have a guard value that you set to done, when you come back to the start at value....

Or, even a better way.

-steve
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Steve:

I did try writing it down in English lol.

I seem to have some difficulty when it comes to figuring out indices. But I couldn't really reach a general formula or some sort of rule mathematically as to how I can tell the progrm to stop.

For example, when I have something like this:



The do loop does run but it doesn't STOP at where starting at items[startingAt-1], which is what shoud do. I tried the inequality symbol >, it did follow through to some degree but stopped when it reached startingAt%items.length = 0.

I need it to keep going until it reaches items[2], and then stop there, because it already did items[3].

S.T.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 372
    
    1

Sam Thompson wrote:

In your current while condition, where are you comparing your index with the initial index??
You see that your startingAt is no longer pointing at the index you started on...
So, you simply cannot base your condition on its value..

So, you need to have a copy of the index where you started and compare to that index..
Something like this:-
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

I think you're getting hung up because you want to solve it with one loop. Why don't you just use two loops? One loop to print from where you want to start, then the other loop to print from beginning to right before you started in the first loop.

And if you're going to ask "Well, isn't that inefficient?" then ask yourself how much time you've spent trying to figure out the condition so you can use only one loop.

"Premature optimization is the root of all evil." -- Donald Knuth


Junilu - [How to Ask Questions] [How to Answer Questions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Also consider whether a do loop is the correct sort of loop for this problem.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

also, note that if your index is '4' , you don't have to print the 4th element of the array.

I would personally use a for-loop, since I know exactly how many times i need to loop (item.length), then just write a little math to figure out the offset to print...
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Sam Thompson wrote:
I know for some of you, you may think it may be more efficient to use a for loop perhaps, but there is a reason I'm using a do-while.

As you can see from the responses so far, being vague and mysterious about your reasons is not helping you get straight answers. Unless you help others understand why, the suggestions are going to lean towards the solution that makes the most sense, which right now is still a for-loop.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

Sam Thompson wrote:I know for some of you, you may think it may be more efficient to use a for loop perhaps, but there is a reason I'm using a do-while.

There is only ONE reason for using a do...while loop - and that is that you ALWAYS want the body of the loop executed at least once.
And even then, you can often do that with a for loop, which is semantically more complete.

I'm actually trying to remember the last time I used do...while. Unfortunately, Alzheimers is getting in the way...

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Hello everyone:

Thank you so much for the input thus far.

It looks like trying to simplify it was actually making things harder. But that's alright. If you need to see the real code, it can be found in another post I have:
http://www.coderanch.com/t/592416/java/java/Method-not-working-or-operating

What I am trying to do in this program is to record data from a poker game from any number of players and store in a .txt or .csv file for later analysis.

The main flow of the program is supposed to be this:
1) The computer asks for how many users, let's say 4.
2) It then asks for the names of those players, call them Moe, Larry, Curly and Shemp.
3) Asks for the first flop of the three cards

From here, the computer asks the user what player is starting the process first, (call him Larry, so the value entered will be 1) and whether or not they are going to 1) RAISE, 2) CALL, or 3) FOLD.

At this point this is where the loop begins. But, as some of you have seen from contributing in this post and my past posts, that I am having difficulty with using a loop for this.
A loop is necessary because 1) it needs to go through ALL the players, from Larry and then back to Moe. The computer should go like this through the loop: Larry, Curly, Shemp, Moe.

At the same time I have been also having difficulty with developing the code for in case Larry decides to raise. What if Curly decides to raise? Then Shemp? Then Moe? There are many times in a poker game where the raising process can continue UNTIL someone finally ends it and calls it. This is where the inner nested do-while loop comes in. Because the process itself is, as you can see, repetitive until someone finally calls it.

So, from what I can see so far a loop is needed for going around the entire set of players, collected and declared under an ArrayList<PokerPlayer> table. The first loop goes through all the players.
The second loop starts at whoever decided to raise FIRST, and then keep going UNTIL someone calls it, which will end the second loop.

The first time I have written the code, I DID use a for loop, but it did not execute or do anything. So I decided to try to use a do-while loop, which DID work, especially since in some cases the process flow of the computer may depend on more than one condition, rather than just the index of table variable itself.

So, having said all of that, lol, what do you think?

I hope this clarifies the problem for everyone. I didn't want to put the whole code set in the post to avoid making anyone feel overwhelmed.

Any further input is welcome and greatly appreciated. Thank you again everyone! I hope to hear more of your ideas soon.

SAM
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

Well let's see....

There are three normal loops...

the for loop (which could work well)

the do... while loop (which is arcane as you have seen)

the the while loop (which is the general case of the for loop and the do...while loop).

Now you need to understand a few concepts....

a do until and do while are the same except you have the negation of the test.

--------

so back to my previous post, and your response.

You have already solved your situation -- you provided a do... until -- just change it to a do while

Hint: as said prior by others -- you need to keep the entry index to test against (or the number of iterations)

so....



Note: in the case you gave, both the while and the for would have been better solutions, but there you have your do...while

-steve
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

By the way - Winston.

I know the last time i wrote a do...while was doing a user selected menu option loop in a command prompt.

I am so glad those days are over.

-steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Now that we have more context, it is making more sense.

I would suggest you not use an array at all. What you need is a circular linked list of players. You keep a reference to the 'dealer'. you then have another reference (possibly called "activePlayer") to the first player after the dealer. After that player acts, you move to the next via a nextActivePlayer() method. you can keep a reference to the last player who raised (lastRaiser?), and if the activePlayer equals the lastRaiser, then the round of betting is done.

A circular linked list has the advantage that you can add in new player or have old ones drop out at any time. I may even put a 'inHand' boolean on each player, and when they fold, set it to 'false'. The nextActivePlayer() method would keep moving to the next player until the inHand is true - thus skipping players who have folded.

When you deal the next hand, each player's inHand gets reset to true.
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Fred:

I totally agree with you, arrays would not be appropriate in this case due to the nature of the game.

What exactly is a circular linked list that you mention exactly? Are they related to ArrayLists at all or are they a total different entity?

Would this work if there was to be multiple raise events during the game? How exactly would you handle the ending of the raising event sequence?

SAM
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

A linked list is a list where each node/element points to the next node/element in the list. A double linked list is one where each node points to the next and previous nodes. Two "special" nodes in a linked list are the head and tail nodes, which have no previous and next nodes, respectively. A circular linked list would be one where the tail references the head as its next node. If you need a circular double linked list, the head would also reference the tail as its previous node.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Sam Thompson wrote:What exactly is a circular linked list that you mention exactly?

I'd start with this.

A cicular linked list is a basic data structure. It is not specific to Java. You can make one in C, perl, .net, or just about any other high-level language.

I'm not sure that there is a pre-built class specifically for this in the API, but I think the plain linked list would work - you'd just need to make the last point to the first when you build it.
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Fred:

Could you give me some ideas on how to construct the circular list?

Maybe like a code example? I am vaguely familiar with the concept but not sure how I would phrase it in code.

Only data structures I'm familiar with so far are arrays, arraylists, multidimensional arrays and the antiqauted and now obsolete data structure of vectors haha.

Thanks

Sam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do-while loop index problem