• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Infinite while loop

 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I have written a while loop to print the values of Home1 object. My ouput should be as follows:

Summary Report for Home # 1
Type: Single Family
Age: 15 YRS
Area: 2000 SQ FT
Value: $300000.0
Commission: 0.06
and it should print the corresponding values for Home #2 and Home #3 but mine is going into an infinite loop.



Can someone please let me know what is wrong with my while loop??

Thank you!!

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never change the value of hTmp so it is never set to null and therefore the while loop runs forever.
 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Tom, Thank you for your quick reply.

I have tried using getter method in the while loop but it didnot work.Can you please let me know how shuld I change the value of hTmp??




Thank you
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
possibly with

hTmp = <something>
 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Fred,

Thank you for your reply.

[code=java][/code]

while(hTmp!=null)
{
h.setnextHome(hTmp);
printReport(hTmp,i);
hTmp=hTmp.getnextHome();
i=i+1;

}
[code=java][/code]

Even now it goes into an infinite loop. I am not knowing what is wrong in my loop.Can you please let me know what is wrong with my code.

Thank you.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the getnextHome() method work? Is that coded to return null?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be quite helpful if you would post the code, at least the relevant code, from your Home1 class; I think we can solve this one without that, however.

My comments pertain to your first post; in it, you have the method in Home1 called setNexthome(Home1 nextHome) which appears to assign 'nextHome' to be 'next' in the linked list. That wouldn't change the value of nextHome, only the value of a variable in the object on which that method is called. So if you have "h.setNextHome(hTmp);", it changes the value of something within h but does not alter (or set) the value of hTmp.

It appears to me you call this correctly twice, setting up a linked list where h -> h1 -> h2.

Your loop to walk this list, however, calls setNextHome again. There is no need to do this; to walk the list, you do not need to set any value in the linked list, assuming that all you're doing is printing values from the list. I think you do want the method mentioned in a subsequent post, namely getNextHome(), as a public method in Home1 returning a Home1 object. This method would return the value of the 'next' home; if there is none, it returns null and you've reached the end of the list. Using this, your loop becomes:



I hope that's enough explanation to go along with the example.

rc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic