943,940 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4205
  • Java RSS
Aug 11th, 2005
0

do while loop - not stopping!!

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  1. public void correctSearchPath(int searchValue){
  2. if (outCome == 0) { // node is IN tree
  3. System.out.println("correctSearchPath node is in tree");
  4. for (i = 0; i < amount; i++){
  5. do {
  6. optimalSearch.add(new Integer(nums[i]));
  7. System.out.println("searchValue is " + searchValue);
  8. }
  9. while (i != searchValue);
  10. }
  11. System.out.println("optimalsearchpath is: " + optimalSearch);
  12. }
  13. if (outCome == 1){ // node NOT in tree
  14. for (i = 0; i < amount; i++){
  15. System.out.println("correctSearchPath node is NOT in tree");
  16. }
  17. }
  18. System.out.println("outcome of flipCoin is " + outCome);
  19. } // method to drawWhole tree

hope someone can help me with this, its driving me and the program loopy :o
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Aug 11th, 2005
0

Re: do while loop - not stopping!!

Well, you never change searchValue inside the loop so if it's not the same as your loop counter you're entering an eternal loop.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 11th, 2005
0

Re: do while loop - not stopping!!

hmm, not sure I understand. Why would I want to change searchvalue? As for each time this method is run it will be a different value but it shouldn't change when the loop is running...?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Aug 11th, 2005
0

Re: do while loop - not stopping!!

Look at the do/while loop all by itself:

Java Syntax (Toggle Plain Text)
  1. do {
  2. optimalSearch.add(new Integer(nums[i]));
  3. System.out.println("searchValue is " + searchValue);
  4. }
  5. while (i != searchValue);
This commands java to loop those two lines until search value != i. Well neither of those two lines up there is going to cause search value to change!! So it loops forever.
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
JeffHeaton is offline Offline
58 posts
since Jul 2005
Aug 11th, 2005
0

Re: do while loop - not stopping!!

I am sorry about this but I just don't get how to fix it. I'm not sure why I would need to change searchValue.

For example, searchValue might be 10 and looking in the following array [8,12,11,10,15]
I would then just want the loop to add [8,12,11,10] to the vector. I don't need to change searchValue.

I am being dim I know but if you could please continue to offer advise on this that would be great
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Aug 11th, 2005
0

Re: do while loop - not stopping!!

Think about what you want to do and how you'd do that if you were to do it with buckets full of marbles.
Write that down, then translate that into code.

Your entire algorithm is wrong, and your description of what you want to do is so vague it seems to me you don't even know yourself what you're trying to achieve here.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 11th, 2005
0

Re: do while loop - not stopping!!

sorry I have not conveyed the problem clearly. I do know what I am trying to do but obviously not explaining it very well and have written down what I wanted and tried to translate it to code - but thats where I always get stuck.

never mind!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java arrays/parameters
Next Thread in Java Forum Timeline: Reading Large Files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC