• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

do while help

 
hyderman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic