File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do while loo help - not stopping...." Watch "do while loo help - not stopping...." New topic
Author

do while loo help - not stopping....

son dunckley
Greenhorn

Joined: Jul 27, 2005
Posts: 17
Hi

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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Greenhorn

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:

15,17,16

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.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do while loo help - not stopping....