File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Infinite while loop" Watch "Infinite while loop" New topic

Infinite while loop

Divya Kotamraju

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

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

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

Joined: Oct 02, 2003
Posts: 11955

possibly with

hTmp = <something>

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

Joined: Oct 07, 2010
Posts: 21
Hello Fred,

Thank you for your reply.




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

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

It is sorta covered in the JavaRanch Style Guide.
subject: Infinite while loop
It's not a secret anymore!