Cannot output Results

Discussion in 'Java' started by shannon, Feb 2, 2006.

  1. shannon

    shannon Guest

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


    }
    }}
     
    shannon, Feb 2, 2006
    #1
    1. Advertising

  2. shannon

    Guest

    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.

    shannon wrote:
    > 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 <" +> PROBLEM IN THIS LINE. Fix in <>
    > <"> 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 );
    >
    >
    > }
    > }}
     
    , Feb 3, 2006
    #2
    1. Advertising

  3. shannon

    shannon Guest

    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
     
    shannon, Feb 3, 2006
    #3
  4. shannon

    Roedy Green Guest

    On 2 Feb 2006 13:43:29 -0800, "shannon" <>
    wrote, quoted or indirectly quoted someone who said :

    > 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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 3, 2006
    #4
  5. shannon

    Roedy Green Guest

    On 2 Feb 2006 13:43:29 -0800, "shannon" <>
    wrote, quoted or indirectly quoted someone who said :

    > 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 3, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manfred
    Replies:
    1
    Views:
    450
  2. Monique Y. Mudama
    Replies:
    1
    Views:
    457
    Monique Y. Mudama
    Jun 28, 2005
  3. lovecreatesbeauty
    Replies:
    8
    Views:
    1,665
    Old Wolf
    Sep 12, 2005
  4. Ken Fine
    Replies:
    3
    Views:
    540
    Steven Cheng [MSFT]
    Jul 23, 2008
  5. IluDeR
    Replies:
    0
    Views:
    250
    IluDeR
    Jan 21, 2004
Loading...

Share This Page