[Logo]
Forums Register Login
do while help
hi
i have a problem with do while in this code


public int find (int value)
{

int counter = 0;
Node tempNode = root;

do {

counter++;
if (value == tempNode.getValue() )
{
this.current = tempNode;
return counter;
}
else if (value < tempNode.getValue() )
{
tempNode = tempNode.getLeft();
//this.current = tempNode;
}
else
{
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.

thanx
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...
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.

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
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.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Oct 17, 2017 11:20:02.