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