Two Laptop Bag
The moose likes Beginning Java and the fly likes do while help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do while help" Watch "do while help" New topic

do while help


Joined: Jun 03, 2004
Posts: 3
i have a problem with do while in this code

public int find (int value)

int counter = 0;
Node tempNode = root;

do {

if (value == tempNode.getValue() )
this.current = tempNode;
return counter;
else if (value < tempNode.getValue() )
tempNode = tempNode.getLeft();
//this.current = tempNode;
tempNode = tempNode.getRight();
//this.current = tempNode;
//current = null;
return -1 * counter;
} while (tempNode != null);

its not running and i am confused where is the problem here.

Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
It is not do while problem...

In such case to check some logical process, see it in debuger,
or add printing (likes System.out.println("go to left..."))
for each logical brach, and then you easy can see how it is working...
Jeff Pavlocak

Joined: Apr 26, 2004
Posts: 16
Can you print out the value of tempNode before you enter the do loop? This way you can see if it really is a null and not a problem with the logic in your loop.
Emmy Rauch

Joined: Jul 07, 2004
Posts: 12

You seem to have an misplaced bracket first of all, I don't see how this could compile like that.
Assuming that bracket is removed, if the value is in the tree but not the node the code will return -1 every time because you have the return statement inside the do-while loop. If you fix these and still have a problem, post again.
Btw, you should indent and use the code tags when you post
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I see various logical problems, but I'm not sure whether you're showing the whole method here, or whether part is cut off at the bottom, so I'm not exactly sure what to recommend.

It's hard to tell because of the lack of formatting, but there's an extra close bracket before the two lines

//current = null;
return -1 * counter;

and that extra bracket is going to keep this code from compiling.

A good programmer's editor or IDE can help you to find this kind of problem as you type. There are several excellent free Java IDEs (Eclipse, NetBeans) and editors like Emacs that would let you know about mistakes like this as soon as it happened. it's worth the small investment in time to learn to use one of these tools.

Finally, note that I've already pointed you to our naming policy once, and asked you to go here and update your display name. Please do so now, before posting again, or I'll have to close your account.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: do while help
It's not a secret anymore!