User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 361,894 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,257 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1907 | Replies: 6
Reply
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Solution do while loop - not stopping!!

  #1  
Aug 11th, 2005
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.

public void correctSearchPath(int searchValue){
    if (outCome == 0) { // node is IN tree
        System.out.println("correctSearchPath node is in tree");
        for (i = 0; i < amount; i++){
        	do {
                optimalSearch.add(new Integer(nums[i]));
                System.out.println("searchValue is " + searchValue);
            }
        	while (i != searchValue);
        }
        System.out.println("optimalsearchpath is: " + optimalSearch);
    }    
    if (outCome == 1){ // node NOT in tree
    	for (i = 0; i < amount; i++){
                 System.out.println("correctSearchPath node is NOT in tree");	
    	}	
    }
    System.out.println("outcome of flipCoin is " + outCome);
} // method to drawWhole tree

hope someone can help me with this, its driving me and the program loopy :o
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,576
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 184
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: do while loop - not stopping!!

  #2  
Aug 11th, 2005
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: do while loop - not stopping!!

  #3  
Aug 11th, 2005
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...?
Reply With Quote  
Join Date: Jul 2005
Posts: 58
Reputation: JeffHeaton is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
JeffHeaton's Avatar
JeffHeaton JeffHeaton is offline Offline
Junior Poster in Training

Re: do while loop - not stopping!!

  #4  
Aug 11th, 2005
Look at the do/while loop all by itself:

   	do {
                optimalSearch.add(new Integer(nums[i]));
                System.out.println("searchValue is " + searchValue);
            }
        	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.
Jeff Heaton
Check out my Java Neural Networks
Reply With Quote  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: do while loop - not stopping!!

  #5  
Aug 11th, 2005
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
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,576
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 184
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: do while loop - not stopping!!

  #6  
Aug 11th, 2005
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: do while loop - not stopping!!

  #7  
Aug 11th, 2005
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 8:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC