| | |
do while loop - not stopping!!
![]() |
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Solved Threads: 0
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 :o
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)
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
Look at the do/while loop all by itself:
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.
Java Syntax (Toggle Plain Text)
do { optimalSearch.add(new Integer(nums[i])); System.out.println("searchValue is " + searchValue); } while (i != searchValue);
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Solved Threads: 0
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
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
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.
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- Problems with While Loop (PHP)
- Loop stopping short (Pascal and Delphi)
- loop and class problem (C++)
- Keyboard input or "stuck in a loop" (C++)
- Loop...without the loop (Java)
Other Threads in the Java Forum
- Previous Thread: Java arrays/parameters
- Next Thread: Reading Large Files
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card class client code collision component crashcourse css csv database eclipse ee error fractal free ftp game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linked linux list loan machine map method methods migrate mobile netbeans objects oriented output phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext scanner se server service set sms software sort sql string swing test textfield threads tree trolltech ubuntu utility windows






