Results 1 to 8 of 8
  1. #1
    SubjectJ80 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Help Needed. New to Java Compound Interest Program....

    Hi,

    I'm new to Java programming and cannot seem to use printf correctly. I have a compound interest program, I have it nearly completed but cannot print in columns...

    Can anyone help.....my output should be like this

    Amount on deposit
    Year 5% 6% 7% 8% 9% 10%
    1 1050.00 1060.00 1070.00 1080.00 1090.00 1100.00
    2 1102.50 1123.60 1144.90 1166.40 1188.10 1210.00
    3 1157.63 1191.02 1225.04 1259.71 1295.03 1331.00
    4 1215.51 1262.48 1310.80 1360.49 1411.58 1464.10
    5 1276.28 1338.23 1402.55 1469.33 1538.62 1610.51
    6 1340.10 1418.52 1500.73 1586.87 1677.10 1771.56
    7 1407.10 1503.63 1605.78 1713.82 1828.04 1948.72
    8 1477.46 1593.85 1718.19 1850.93 1992.56 2143.59
    9 1551.33 1689.48 1838.46 1999.00 2171.89 2357.95
    10 1628.89 1790.85 1967.15 2158.92 2367.36 2593.74

    But I'm getting this:

    Amount on Deposit
    Year 5% 6% 7% 8% 9% 10%
    1 1,050.00
    2 1,102.50
    3 1,157.63
    4 1,215.51
    5 1,276.28
    6 1,340.10
    7 1,407.10
    8 1,477.46
    9 1,551.33
    10 1,628.89

    Code:
    Java Code:
    public class InterestRates {
    
    
        public static void main(String[] args) 
        
        {
            double amount,amount1, amount2, amount3, 
                    amount4, amount5;// amount on each deposit at the end of the year
            double principal = 1000.0; // amount before interest
            double rate = 0.05, rate1 = 0.06, rate2 = 0.07,rate3 = 0.08,
                    rate4 = 0.09, rate5 = 0.10; // all interest rates
            
            // display headers
            System.out.println("Amount on Deposit");
            System.out.printf("%4s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t\n", 
                    "Year", "5%", "6%", "7%", "8%", "9%", "10%");
            
            // calculate the amount on deposit for 5% rate for 10 years
            for( int year = 1; year <= 10; year++ )
            {
                // calculate the new amount for specified year
                amount = principal * Math.pow( 1.0 + rate, year );
                amount1 = principal * Math.pow( 1.0 + rate1, year );
                amount2 = principal * Math.pow( 1.0 + rate2, year );
                amount3 = principal * Math.pow( 1.0 + rate3, year );
                amount4 = principal * Math.pow( 1.0 + rate4, year );
                amount5 = principal * Math.pow( 1.0 + rate5, year );
                
                // display the year and the amount for the 5% rate
                System.out.printf("%4d%,20.2f\t\n", year, amount, amount1, 
                        amount2,amount3, amount4, amount5);
                
            }
                
        }
    }
    Can anyone show me what I am doing wrong??? Or just give me some advice, my code might be a bit crappy!!

    Many Thanks.....
    Last edited by SubjectJ80; 11-03-2011 at 07:11 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help Needed. New to Java Compound Interest Program....

    When posting code use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the formatting will be preserved when the code is displayed on a web page. The same goes for the tables.

    Java Code:
    System.out.printf("%4d%,20.2f\t\n", year, amount, amount1, 
            amount2,amount3, amount4, amount5);
    This is only displaying the year and one of the amounts. SeeFormat string syntax in the API docs. And Formatting in Oracle's Tutorial.

  3. #3
    SubjectJ80 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Help Needed. New to Java Compound Interest Program....

    Thank you for the info. I have added the code tags now. So I need to format it as a string and not use printf??

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help Needed. New to Java Compound Interest Program....

    No, you use printf(), but you need to replace "%4d%,20.2f\t\n" with a format string that will display all of the amounts.

  5. #5
    SubjectJ80 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Help Needed. New to Java Compound Interest Program....

    Really sorry about this, but I don't know how to do that. I've looked over the Format string syntax in the API docs. And Formatting in Oracle's Tutorial and I'm none this wiser?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help Needed. New to Java Compound Interest Program....

    Fair enough. They can be cryptic, but it's worth learning the bits you need as you come across the need because these format string (and similar) exist across lots of languages (C, Java, PHP etc).

    %4d
    % marks this as being a format instuction. Each %... corresponds to one of the arguments that follows the string.
    d is for a decimal (base 10) integer
    4 means four characters wide.

    You can also use a zero and a minus sign to adjust formatting. This is most easily seen with an example. (The | symbol is just there so you can see the padding)

    Java Code:
    public class FormEg1 {
        public static void main(String[] args) {
            int year = 2011;
    		
                // multiple % for multiple arguments
            System.out.printf("%6d %6d %6d\n", year, year + 1, year + 2);
                // - for left aligned, 0 for pad with zeros
            System.out.printf("|%6d|\n", year);
            System.out.printf("|%-6d|\n", year);
            System.out.printf("|%06d|\n", year);
        }
    }
    %20.2f
    f is for floating point numbers
    20 means 20 characters wide
    .2 means rounded to 2 decimal places

    Again there are variants including , which makes comma the thousands separator. Note also that %n can be used for newline.

    Java Code:
    public class FormEg2 {
        public static void main(String[] args) {
            double value = Math.PI * 1000;
                // padding and alignment as before
            System.out.printf("|%9.2f|%n", value);
            System.out.printf("|%09.2f|%n", value);
            System.out.printf("|%-9.2f|%n", value);
                // thousands
            System.out.printf("|%,9.2f|%n", value);
                // again, multiple % for multiple values...
            System.out.printf(
                    "%12.2f%12.2f%12.2f%12.2f", 
                    value, value * 10, value * 100, value * 1000);
        }
    }

  7. #7
    SubjectJ80 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Help Needed. New to Java Compound Interest Program....

    Scratch that. I finally got. Thank you so much for all your help. Your good deed is done!!
    Last edited by SubjectJ80; 11-05-2011 at 06:27 AM.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help Needed. New to Java Compound Interest Program....

    You're welcome.

Similar Threads

  1. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 05:36 AM
  2. Calculating compound interest using ONLY integers.
    By AprilFlowers in forum New To Java
    Replies: 2
    Last Post: 10-21-2009, 07:47 AM
  3. Help needed very simple java program
    By pavez in forum New To Java
    Replies: 3
    Last Post: 08-22-2009, 04:05 PM
  4. Help with compound interest program pleaze
    By Smirre in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 10:31 PM
  5. How to use compound property names in Spring
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •