• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with nested loop  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Java Cowboy
Posts: 16079
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

I can't take it! You are too smart for me! Here is the tiny ad:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!