aspose file tools*
The moose likes Beginning Java and the fly likes Infinite while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Infinite while loop" Watch "Infinite while loop" New topic
Author

Infinite while loop

Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
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

Joined: Jun 01, 2010
Posts: 618
You never change the value of hTmp so it is never set to null and therefore the while loop runs forever.
Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
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

Joined: Oct 02, 2003
Posts: 10908
    
  12

possibly with

hTmp = <something>


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
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

Joined: Aug 31, 2004
Posts: 11343

How does the getnextHome() method work? Is that coded to return null?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Infinite while loop
 
Similar Threads
Money and house...
Please explain the output
Preventing Divide by zero exception
Why is this hashmap iterating through an infinite loop?
Puzzling question