• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

do while help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic