Cannot output Results

S

shannon

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 );


}
}}
 
S

sillsm

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.
 
S

shannon

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
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top