aspose file tools
The moose likes Beginning Java and the fly likes do while loo help - not stopping.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do while loo help - not stopping...." Watch "do while loo help - not stopping...." New topic

do while loo help - not stopping....

son dunckley

Joined: Jul 27, 2005
Posts: 17

I am having a bit of trouble with a do while loop. I either can't get it to stop, or it doesn't do exactly as I need it to.

I am using a array called nums to insert random numbers into a binary search tree. Once the tree has been created I need to create a method to search the nums array looking for the node I have asked the user to find in the tree. Once it finds it I need it to output the nodes in that order into a vector.

Sometimes I ask for a node which is not in the tree - this is represented by the outCome. 0 if it is in the tree and 1 if it is not.

I'm attempting to do the first part, if the node is in the tree and have tried creating the following method. Currently it just loops. Can someone tell me how to tell it to stop?

Also, could you help me with telling it to only add nodes until it reaches the node they are searching for, represented by searchValue.

hope someone can help me with this, its driving me and the program loopy
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Let's just look at this part for now:

For each value of i from 0 to amount-1, we'll start this do-loop. We'll add an item to optimalSearch, print a message, and then go back to the top of the do-while loop, if i isn't equal to the searchValue. Do you see the problem? The do-while is an infinite loop, because neither searchValue nor i changes during its execution. It will loop forever, and nothing will ever change.

I honestly can't tell what you intend, exactly, so I'm not quite sure what to recommend instead. But I really only see one loop variable here, so it seems like you only need one loop (the for loop), not both.

[Jess in Action][AskingGoodQuestions]
son dunckley

Joined: Jul 27, 2005
Posts: 17
Thanks for the response. I have tried using the for loop only but it then displays the whole array.

What I am trying to do is, if the nums array contained [15,17,16,25,22]
and the user was looking for [16]. I would like to search the array and then stop at 16 and add all those previous numbers to the vector, so it would contain:


including the one they were looking for.

Does that make more sense?

Its just I need to be able to inform the user the best search root if they didn't take it.

subject: do while loo help - not stopping....