This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with nested loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with nested loop" Watch "Help with nested loop" New topic
Author

Help with nested loop

Jim Hackney
Greenhorn

Joined: Apr 17, 2011
Posts: 1
Hello all. This is my first time posting here. I am hoping to get some advice on a homework program I am doing. I am completely lost on this particular program. I am finding nested loops to be very confusing. I tend to "get lost" in them. Here is what I am doing:

I am supposed to write a program that finds the monthly payment for 30 years, for principals from $100,000 through $200,000(with increments of $20,000) and interest rates of 6% through 10% (with increments of 0.5%). The output of the file must be in a table that looks something like this:

Principal 6% 6.5% 7% 7.5% 8% 8.5% 9% 9.5% 10%
100000 600 632 665 699 733 768 804 840 877
120000 719 758 798 839 880 922 965 1009 1053
140000 … … … … … … … … …
160000 … … … … … … … … …
180000 … … … … … … … … …
200000 … … … … … … … … …

The monthly payment on mortgage loan of L dollars, at a rate of interest "r" is given by:

Monthly payment = [L(1 + r/12)exponent 12N ]/[(1 + r/12)exponent 12N - 1]

where N = number of year mortgage (30 years).

The professor gave us this hint:



I have tried adding his loop as the first loop, the second loop and the last loop to no avail. I've tried putting the last System.out.println(monthlyPayment) within the first for loop curly bracket, the second and the last. I am completely at a loss. Here is the latest I tried:



I would appreciate it if someone could steer me the right direction.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Welcome to the Ranch!

The first loop is for the rows (line 17); it goes from 100,000 to 200,000 with increments of 20,000.

The second loop is for the columns in each row (line 21), it goes from .06 to .101 in increments of .005.

The printout in line 29 is in the wrong place. It should be inside the inner loop, but now it is not. Put this line between line 27 and 28.

Furthermore, you're printing newlines in some places in such a way that you don't get a neat table output as you intend. You only want to print a newline at the end of a row - not after every number.

In line 18, you don't want to print newlines. Remove the "\n", and change println() to print(). Maybe you want to print some spaces after the number L, so change this to something like:

Also, in line 29 (that should be moved to between 27 and 28) you also don't want to print a newline. Change this to something like:

Then at the end of each row you want to print a newline. Put this in line 29 (outside the inner loop, but inside the outer loop):

That takes care of the formatting, but then there's still something wrong with the calculations (I get a table full of zeroes when I run it). Check carefully, by following the program line by line in your head, keeping track of the values of the variables, what it's actually calculating, and if this is what you expect or not.

About this code (lines 12-14):

Why did you write it that way? You could just write it out as one string:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with nested loop