Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Cannot output Results

Hi,

Having problems outputting the following text. I had the text outputting before i added the scroll bar functionality, I can't figure out where i went wrong. Maybe someone can help. thanks in advance. 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 ");


//calculate amount of deposit for each of ten years
for ( int year = 1; year <= 10; year ++ ) {
//calculate interest rate
for ( int rate = 5; rate <= 10; rate ++ ) {
//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" + 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
}

outputArea.setText( output );

JOptionPane.showMessageDialog( null, scroller, "Results Table", JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );


}
}}
[2175 byte] By [ShanD] at [2007-11-11 7:54:52]
# 1 Re: Cannot output Results
When you use the line

outputArea.setText( output );

At the end of the program, it will erase everything you've appended to it previously.
srekcus at 2007-11-11 22:36:59 >
# 2 Re: Cannot output Results
Thanks for that. Now experiencing another problem. The output is only displaying the info for year 10. My for loop seems ok. Only started java a couple of weeks ago.
ShanD at 2007-11-11 22:37:59 >
# 3 Re: Cannot output Results
Sounds like your writes to your TextArea are NOT being appended to the existing text - the new year's output is replacing the past year's output, and on and on until only the 10th year is there.

So ... how do you append text to the existing text?
nspils at 2007-11-11 22:38:58 >
# 4 Re: Cannot output Results
ok, have been working on this and i think its nearly there except that all my years and interest rate results are going down in one line instead of the years down and the rates across. The result figures are now right but the display isnt.

Sorry the code is a bit messy have been blocking out stuff and putting it back in while trying to get this right.

I really appreciate the help.

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\n\t " + "deposit\t5%\t6%\t7%\t8%\t9%\t10%\n ");
outputArea.setText( output );

for ( double rate = .05; rate <= .10; rate += 0.01 ) {
for ( int year = 1; year <= 10; year ++ ) {



//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 ) + "\n" );
// "\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 );


}
}}
ShanD at 2007-11-11 22:40:08 >
# 5 Re: Cannot output Results
Your code outputs on my computer in the fashion that I would expect from your code ...

Can you explain what is missing (did you remember about your commenting out of the other values?

By the way - how are you going to differentiate what "amount" you're talking about in the output of the various rate columns?
nspils at 2007-11-11 22:41:01 >