Hi,
Having problems trying to output my code, I figure that there is
something wrong with the for loops but can't get my head around it.
Would appreciate some help . thanks Shannon
import javax.swing.*;
import java.text.NumberFormat;
import java.util.Locale;
public class ScrollBar {
public static void main( String args[] ) {
{
double amount;
double principal = 1000.0;
//create NumberFormat for currency in US dollar Format
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance(
Locale.US );
JTextArea outputArea = new JTextArea( 17, 50 );
JScrollPane scroller = new JScrollPane( outputArea );
String output = ( "Year\tAmount on
deposit\t5%\t6%\t7%\t8%\t9%\t10%\n ");
for ( double year = 1; year <= 10; year ++ ) {
//calculate amount of deposit for each of ten years
for ( double rate = .05; rate <= .10; rate += 0.01 ) {
//calculate interest rate
outputArea.setText( output );
//calculate new amount for specified year
amount = principal * Math.pow( 1.0 + rate, year);
//append one line of text to outputArea
outputArea.append( year + "\t" + principal + "\t" +
moneyFormat.format( amount ) +
"\t" + moneyFormat.format( amount
) +
"\t" + moneyFormat.format( amount
) +
"\t" + moneyFormat.format( amount
) +
"\t" + moneyFormat.format( amount
) +
"\t" + moneyFormat.format( amount
) + "\n" );
} //end for
}
JOptionPane.showMessageDialog( null, scroller, "Results",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}}
sillsm@gmail.com - 03 Feb 2006 00:40 GMT
Shannon, I've compiled your code and it runs fine from the command
line. I think your problem was an unclosed string literal. You had a
string which cut off onto a new line, without closing it in quotation
marks and using +. I've made this adjustment to the code, in capital
letters down there so you can see what went wrong.
> Hi,
> Having problems trying to output my code, I figure that there is
[quoted text clipped - 58 lines]
> }
> }}
shannon - 03 Feb 2006 09:10 GMT
Thanks for that. I'm still having problems. I think I have a problem
with my embedded for loop as the interest rate does not change over the
6 different interest rates and It keeps appending the text so i end up
with year 10 all the time
Roedy Green - 03 Feb 2006 11:45 GMT
> for ( double rate = .05; rate <= .10; rate += 0.01 ) {
for counting use ints, and the compute your rate from the int, or
increment each time around. Otherwise you will go one too short or
too far sometimes.
see http://mindprod.com/jgloss/floatingpoint.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 03 Feb 2006 12:00 GMT
> amount = principal * Math.pow( 1.0 + rate, year);
to understand what you are doing to yourself,
System.out.println( amount );
also rate and principal every time through the loop.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.