# Data Table HELP!

Page 1 of 1

## 7 Replies - 776 Views - Last Post: 04 May 2010 - 04:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=171933&amp;s=5abae5396478a4e7bc3ca7669ae17052&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Celesteon

Reputation: 0
• Posts: 24
• Joined: 12-January 10

# Data Table HELP!

Posted 04 May 2010 - 07:14 AM

Hey, so I have an assignment in class:

1. Prompt the user for the amount of an initial investment, the number of years, and the range of interest rates. Then print a table of the value of an initial investment over the range of years at the range of interest rates. The table should look similar to the following for an initial investment of \$1000, invested for 5 years.

1. Years
a. 1 2 3 4 5
ii. Interest Rate
iii. 5% \$1051 \$1105 \$1161 \$1221 \$1284
iv. 6% \$1061 \$1127 \$1197 \$1271 \$1349
v. 7% \$1072 \$1150 \$1233 \$1323 \$1419
vi. 8% \$1083 \$1173 \$1271 \$1377 \$1491
vii. 9% \$1094 \$1197 \$1309 \$1433 \$1568

Remember that you should calculate the value of the investment compounded daily for 1 year. So for 365 days, we will multiply the current value of the investment by the interest rate / 365 and add that back into the value of investment. You will need to use nested loops to build the table.

Here is the code I have so far:

```import java.util.Scanner;
public class  Multitable
{
public static void main (String Args [])
{
double r;
int n;
int k;
double rate1;
double rate2;
int years;
double value;
double newrate;
double val;
double compound;
double sum;

Scanner in = new Scanner (System.in);

System.out.print("Please enter a starting value (\$):");
value=in.nextDouble();

System.out.print("Please enter a starting rate (%):");
rate1=in.nextDouble();

rate2= in.nextDouble();

System.out.print("Please enter the number of years:");
years = in.nextInt();

for (int l=1; l<=years; l++)
{
System.out.print("\t  "+l+"   ");
value = value;
}
System.out.println();

for (int l=1; l<=years; l++)
{
rate1 = rate1;
rate2 = rate2;
for (r=rate1; r<=rate2; r++)
{
if (r<10)
System.out.print(" "+r+"%"+" ");
if (r>=10)
System.out.print(r+"%"+" ");

newrate = r/100.0/365.0;

for (k = 1; k<=365; k++)
{
value = value*newrate+value;
}
System.out.printf("%.0f ", value);
System.out.println();
}

}
}
}
```

Basically, it compiles,and it does a calculation - but I am having a couple problems. I have been testing the formatting of the table, and I've been getting this: (when I test the rate of 5%)

--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value (\$):1000
Please enter a starting rate (%):5
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
5.0% 1105
5.0% 1162
5.0% 1221
5.0% 1284

Process completed.

The values are right (save the third one,but that's a rounding error?)
But I can't manage to get it to go in a line across the top...and when I do attempt that, I get the line, but everything has 5.0% next to it.

Also, when I test for more rates, the values get messed up. I don't really know how to rest my variable each time I go through....I need to reset value to the user entered value every time the program starts over for a new rate? How do I do that? It's annoying though, because even my first values get messed up (for 5%) when they work on their own.

--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value (\$):1000
Please enter a starting rate (%):5
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
6.0% 1116
5.0% 1173
6.0% 1246
5.0% 1310
6.0% 1391
5.0% 1462
6.0% 1553
5.0% 1632
6.0% 1733

Process completed.

Is This A Good Question/Topic? 0

## Replies To: Data Table HELP!

### #2 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,619
• Joined: 15-May 08

## Re: Data Table HELP!

Posted 04 May 2010 - 07:39 AM

Quote

The values are right (save the third one,but that's a rounding error?)
But I can't manage to get it to go in a line across the top...and when I do attempt that, I get the line, but everything has 5.0% next to it.

Bit confused what you mean by the line across the top, please post the output you are seeking to attain.

Cheers

### #3 Celesteon

Reputation: 0
• Posts: 24
• Joined: 12-January 10

## Re: Data Table HELP!

Posted 04 May 2010 - 07:42 AM

The output that I want looks like this:

..................1.........2..........3..........4..........5
5%......\$1051...\$1105...\$1161...\$1221...\$1284
6%......\$1061...\$1127...\$1197...\$1271...\$1349
7%......\$1072...\$1150...\$1233...\$1323...\$1419
8%......\$1083...\$1173...\$1271...\$1377...\$1491
9%......\$1094...\$1197...\$1309...\$1433...\$1568

The "." are spaces...haha...I just can't figure out how to space it out

This post has been edited by Celesteon: 04 May 2010 - 07:47 AM

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,165
• Joined: 27-December 08

## Re: Data Table HELP!

Posted 04 May 2010 - 03:18 PM

You amy find a couple well placed uses of the tab (\t) escape sequence useful. So if I have: System.out.println("Hello\tworld");, that will print something along the lines of the following with the tab in between:
```Hello    World

```

### #5 Celesteon

Reputation: 0
• Posts: 24
• Joined: 12-January 10

## Re: Data Table HELP!

Posted 04 May 2010 - 04:35 PM

macosxnerd101, on 04 May 2010 - 02:18 PM, said:

You amy find a couple well placed uses of the tab (\t) escape sequence useful. So if I have: System.out.println("Hello\tworld");, that will print something along the lines of the following with the tab in between:
```Hello    World

```

I don't mean to sound rude, but that's not really the bit I need help with. I really need help with where to put the print for the rates, considering I'm having trouble getting them to print in that table formation.

I also need help figuring out how to reset a variable...do you know how to do that?

### #6 Celesteon

Reputation: 0
• Posts: 24
• Joined: 12-January 10

## Re: Data Table HELP!

Posted 04 May 2010 - 04:41 PM

Hey, so I have an assignment in class:

1. Prompt the user for the amount of an initial investment, the number of years, and the range of interest rates. Then print a table of the value of an initial investment over the range of years at the range of interest rates. The table should look similar to the following for an initial investment of \$1000, invested for 5 years.

1. Years
a. 1 2 3 4 5
ii. Interest Rate
iii. 5% \$1051 \$1105 \$1161 \$1221 \$1284
iv. 6% \$1061 \$1127 \$1197 \$1271 \$1349
v. 7% \$1072 \$1150 \$1233 \$1323 \$1419
vi. 8% \$1083 \$1173 \$1271 \$1377 \$1491
vii. 9% \$1094 \$1197 \$1309 \$1433 \$1568

Remember that you should calculate the value of the investment compounded daily for 1 year. So for 365 days, we will multiply the current value of the investment by the interest rate / 365 and add that back into the value of investment. You will need to use nested loops to build the table.

Here is the code I have so far:

```import java.util.Scanner;
public class  Multitable
{
public static void main (String Args [])
{
double r;
int n;
int k;
double rate1;
double rate2;
int years;
double value;
double newrate;
double val;
double compound;
double sum;

Scanner in = new Scanner (System.in);

System.out.print("Please enter a starting value (\$):");
value=in.nextDouble();

System.out.print("Please enter a starting rate (%):");
rate1=in.nextDouble();

rate2= in.nextDouble();

System.out.print("Please enter the number of years:");
years = in.nextInt();

for (int l=1; l<=years; l++)
{
System.out.print("\t  "+l+"   ");
value = value;
}
System.out.println();

for (int l=1; l<=years; l++)
{
rate1 = rate1;
rate2 = rate2;
for (r=rate1; r<=rate2; r++)
{
if (r<10)
System.out.print(" "+r+"%"+" ");
if (r>=10)
System.out.print(r+"%"+" ");

newrate = r/100.0/365.0;

for (k = 1; k<=365; k++)
{
value = value*newrate+value;
}
System.out.printf("%.0f ", value);
System.out.println();
}

}
}
}
```

Basically, it compiles,and it does a calculation - but I am having a couple problems. I have been testing the formatting of the table, and I've been getting this: (when I test the rate of 5%)

--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value (\$):1000
Please enter a starting rate (%):5
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
5.0% 1105
5.0% 1162
5.0% 1221
5.0% 1284

Process completed.

The values are right (save the third one,but that's a rounding error?)
But I can't manage to get it to go in a line across the top...and when I do attempt that, I get the line, but everything has 5.0% next to it.

Also, when I test for more rates, the values get messed up. I don't really know how to rest my variable each time I go through....I need to reset value to the user entered value every time the program starts over for a new rate? How do I do that? It's annoying though, because even my first values get messed up (for 5%) when they work on their own.

--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value (\$):1000
Please enter a starting rate (%):5
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
6.0% 1116
5.0% 1173
6.0% 1246
5.0% 1310
6.0% 1391
5.0% 1462
6.0% 1553
5.0% 1632
6.0% 1733

Process completed.

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,165
• Joined: 27-December 08

## Re: Data Table HELP!

Posted 04 May 2010 - 04:45 PM

Have you heard of the Compounding Interest Formula? If not, the formula to determine total payment over a period of time is: Principal * (1 + rate/numTimesCompounded)^(years * numTimesCompounded). I've written a snippet implementing this, which you may find helpful.

As for the implementation of your code, I'd revise it a little bit. Since you want to show a table based on varying rates and years ellapsed, I would use nested loops. So read in your two rates (start and end), then loop from start to end. Inside there, loop from 1 to your lastYear, and plug in data to my snippet and print the result. So something along this setup:
```//maybe loop up here to print out years first

for( ; rate1 < rate2; rate1 += .01){ //increase by 1% each iteration

//print out rate

for(int year = 1; year < maxYear; year++){
//plug in data to my snippet
//and print out the total Amount
//returned
}
}

```

http://www.dreaminco...snippet5060.htm

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,165
• Joined: 27-December 08

## Re: Data Table HELP!

Posted 04 May 2010 - 04:54 PM

Please respect rule #5 of this forum. May the 4th Be With You sounds like a title in a Lounge thread, not a descriptive title for your problem in the Java forum (or any other non-lounge forum for that matter).

Also, you may want to check out this link for more help.