• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do-while loop index problem

 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think it's that simple?
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:-
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also consider whether a do loop is the correct sort of loop for this problem.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
https://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
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic