1. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

## output format problem.

I need my output to look 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 … … … … … … … … …

I'm not sure how to accomplish it. Here is what I have:

Java Code:
```public class ProgrammingProblemTwo {

public static void main(String[] args){

//declare variables
double r = 0, N = 30, monthlyPayment = 0;

double sum;

System.out.println("Principal 6" + "%" + "  6.5" + "%" + "  7" + "%" +
"  7.5" + "%" + "  8" + "%" + "  8.5" + "%" + "  9" + "%" +
"  9.5" + "%" + "  10" + "%\n");

//create loop to increment principal in dollars, L
for(int L = 100000; L <= 200000; L += 20000){
System.out.println(L);
monthlyPayment = 0;

//create loop to increment interest rate, r
for(r = .06; r <= .10; r += .005){
double temp = 1 + r / 12;
double temp1 = 1;

//create (1 + r / 12) exponent 12N formula
for (int i = 1; i<=12*N;i++){
temp1 = temp1*temp;
}
monthlyPayment = (L*(r/12)*temp1)/(temp1-1);
System.out.printf("       %.0f", monthlyPayment);
}
}
}
}```
I am having two problems that I see so far. The first is that the monthly payments are one line below where they need to be. The second problem is that there is too much of a gap between the monthly payments, so they wrap around.

So I need to figure out how to move System.out.printf(" %.0f", monthlyPayment); up one line.

I also need to figure out how to keep the gap for the first monthly payment but bring the other monthly payments closer together so that they somewhat fit underneath the percentages.

2. First off, don't do this:
Java Code:
`System.out.printf("       %.0f", monthlyPayment);`

as you're not using printf to it's full formattingadvantage. Rather, do something like:

Java Code:
`System.out.printf("%6.0f", monthlyPayment); // the 6 may need to be changed to diff number`

or

Java Code:
`System.out.printf("%-6.0f", monthlyPayment); // again the -6 may need to be bigger`

so you would let printf set the width of the data displayed.

Next, you do know that this:
Java Code:
`System.out.println(L);`
will print the value of L and then a carriage return, so you're putting a carriage return at the beginning of the line. Are you sure you want to do this? Rather, consider using printf for this output as well. Just be sure to do a println at the end of the line.

3. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Next, you do know that this:
Java Code:
`System.out.println(L);`
will print the value of L and then a carriage return, so you're putting a carriage return at the beginning of the line. Are you sure you want to do this? Rather, consider using printf for this output as well. Just be sure to do a println at the end of the line.
Yes, I put in the carriage return so that L would be printed like this:

100000
120000
140000
160000
180000
200000

Originally Posted by Fubarable
First off, don't do this:
Java Code:
`System.out.printf("       %.0f", monthlyPayment);`

as you're not using printf to it's full formattingadvantage. Rather, do something like:

Java Code:
`System.out.printf("%6.0f", monthlyPayment); // the 6 may need to be changed to diff number`

or

Java Code:
`System.out.printf("%-6.0f", monthlyPayment); // again the -6 may need to be bigger`

so you would let printf set the width of the data displayed.
Ok, I see what you are saying I went ahead and changed it. However, the problem that I am running into is that I have to have the width 12 spaces in order to make it appear to the right of L. This gives everything a width of 12 spaces which causes it to wrap around. How do I keep the width of 12 for the first monthlyPayment value but substantially shrink the width of the rest of them?

Here's what I have after the change:

Java Code:
```public class ProgrammingProblemTwo {

public static void main(String[] args){

//declare variables
double r = 0, N = 30, monthlyPayment = 0;
double sum;

//output
System.out.println("Principal 6" + "%" + "  6.5" + "%" + "  7" + "%" +
"  7.5" + "%" + "  8" + "%" + "  8.5" + "%" + "  9" + "%" +
"  9.5" + "%" + "  10" + "%\n");

//create loop to increment principal in dollars, L
for(int L = 100000; L <= 200000; L += 20000){
System.out.println(L);
monthlyPayment = 0;

//create loop to increment interest rate, r
for(r = .06; r <= .10; r += .005){
double temp = 1 + r / 12;
double temp1 = 1;

//create (1 + r / 12) exponent 12N formula
for (int i = 1; i<=12*N;i++){
temp1 = temp1*temp;
}
monthlyPayment = (L*(r/12)*temp1)/(temp1-1);
System.out.printf("%12.0f", monthlyPayment);
}
}
}
}```

4. The 6 or 12 is how wide a cell in the table will be. you you want some columns to be 12 chars wide and some to be 8 chars wide then you need to use different printf statements or use a variable.

5. Originally Posted by jim01
Yes, I put in the carriage return so that L would be printed like this:

100000
120000
140000
160000
180000
200000
And you wonder why the numbers are one line below where they should be! Again, use printf for these numbers.

Ok, I see what you are saying I went ahead and changed it. However, the problem that I am running into is that I have to have the width 12 spaces in order to make it appear to the right of L. This gives everything a width of 12 spaces which causes it to wrap around. How do I keep the width of 12 for the first monthlyPayment value but substantially shrink the width of the rest of them?
Don't use 12 for all of the printf constants. You can use multiple constants and variables for printf, you know.

i.e.,
Java Code:
`System.out.printf("%12d %-6.1f %-6.1f%n", myInt, myDouble1, myDouble2);`

6. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by Fubarable
And you wonder why the numbers are one line below where they should be! Again, use printf for these numbers.
I will keep messing with it to see if I can figure it out.

Don't use 12 for all of the printf constants. You can use multiple constants and variables for printf, you know.

i.e.,
Java Code:
`System.out.printf("%12d %-6.1f %-6.1f%n", myInt, myDouble1, myDouble2);`
First off, I would like to thank you for your help. I know it can be very trying dealing with knuckle-headed newbies like myself.

I knew that you could use multiple constants with printf, but I've only done that with simple math programs. In this case all of my output is coming from the nested loop. I have one output constant generating six iterations of output. I don't know how to isolate each iteration and make it a separate constant. I didn't even know you could do such a thing.

7. Make your life easier. Create a method to calculate the interest. It has two parameters (principle, rate) and returns the interest. Then your print statement becomes
Java Code:
`System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, methodCall, methodCall, methodCall, etc);`
Actually it would be even better to use a loop and array. Then the printf statement becomes
Java Code:
```loop {
calculate the interest
}
System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, array[0], array[1], etc);```
Last edited by Junky; 04-18-2011 at 03:45 AM.

8. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by Junky
Make your life easier. Create a method to calculate the interest. It has two parameters (principle, rate) and returns the interest. Then your print statement becomes
Java Code:
`System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, methodCall, methodCall, methodCall, etc);`
Actually it would be even better to use a loop and array. Then the printf statement becomes
Java Code:
```loop {
calculate the interest
}
System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, array[0], array[1], etc);```
We won't learn about making new methods for another 6 weeks. However, we start arrays tomorrow. I've been hearing a lot about arrays lately so I am looking forward to reading that chapter.

9. We won't learn about making new methods for another 6 weeks. However, we start arrays tomorrow. I've been hearing a lot about arrays lately so I am looking forward to reading that chapter.
You know, it wouldn't harm you to read further before hand. In fact it'll only make you understand things faster because when you go over it again in your class it'll be like revision.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•