Results 1 to 8 of 8
  1. #1
    rjanisze is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Java Calculate Future Investment Amount

    I do not know where I'm going wrong here. Pleaqse take a look and let me know what suggestions you have. Thank you. #So Frustrated

    import java.util.Scanner;


    public class FutureInvestment {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub


    Scanner input = new Scanner(System.in);

    //Obtain Investment Amount a
    System.out.println("Enter investment amount: ");
    double a = input.nextDouble();

    //Obtain Interest Rate r
    System.out.println("Enter interest rate: ");
    double r = input.nextDouble();

    //Obtain number of Years y
    System.out.println("Enter number of years: ");
    double y = input.nextDouble();

    //Compute Future Investment Amount f
    double f = a * Math.pow(1 + r, (y * 12));

    //Display result
    System.out.println("Accumulated value is " + f);

    }
    }

    OUTPUT IS...........................

    Enter investment amount:
    1000
    Enter interest rate:
    .0425
    Enter number of years:
    1
    Accumulated value is 1647.8313602236476

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Java Calculate Future Investment Amount

    We have no idea what is wrong unless you tell us. Are you getting incorrect output? Then you should post what you expect the output should be. If it is wrong then most likely your equation is incorrect. Where did you get it? Are you sure you entered it into your code correctly?

  3. #3
    rjanisze is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java Calculate Future Investment Amount

    Expected Output is:
    Enter investment amount: 1000
    Enter annual interest rate: 4.25
    Enter number of years: 1
    Accumulated value is 1043.34

    Actual OUTPUT IS...........................

    Enter investment amount: 1000
    Enter interest rate: .0425
    Enter number of years: 1
    Accumulated value is 1647.8313602236476

  4. #4
    java.java is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Java Calculate Future Investment Amount

    There is a difference between 4.25 and .0425 , please correct your input data and validate the output. If the equation you are using is correct then you should get the same output.

  5. #5
    rjanisze is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java Calculate Future Investment Amount

    Enter investment amount:
    1000
    Enter interest rate:
    4.25
    Enter number of years:
    1
    Accumulated value is 4.384414858452464E11

    The r must be input .01...etc

  6. #6
    rjanisze is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default How do I do a Hard Truncate the output in this Java code after the second decimal?

    Please help Me. My instructions are at the bottom.

    import java.util.Scanner;


    public class FutureInvestment {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub


    Scanner input = new Scanner(System.in);

    //Obtain Investment Amount a
    System.out.println("Enter investment amount: ");
    double a = input.nextDouble();

    //Obtain Monthly Interest Rate r
    System.out.println("Enter interest rate: ");
    double r = input.nextDouble();

    //Obtain number of Years y
    System.out.println("Enter number of years: ");
    double y = input.nextDouble();

    double f = a * Math.pow(1 + (r/12),(y *12));

    //Display result
    System.out.println("Accumulated value is " + f);

    }
    }

    OUTPUT IS
    Enter investment amount: 1000
    Enter interest rate: .0325
    Enter number of years: 1
    Accumulated value is 1032.9885118105894


    I want it to say 1032.98

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

    Default Re: How do I do a Hard Truncate the output in this Java code after the second decimal

    Have a read of the options discussed in Oracle's Tutorial: Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    Accumulated value is 1032.9885118105894


    I want it to say 1032.98
    This sort of rounding where you simply truncate (== throw away insignificant digits) can be done using a RoundingMode.

    Java Code:
    import java.math.RoundingMode;
    import java.text.DecimalFormat;
    
    public class Test {
        public static void main(String[] args) {
            DecimalFormat fmt = new DecimalFormat(".000");
            fmt.setRoundingMode(RoundingMode.DOWN);
                // prints 3.141
            System.out.println(fmt.format(Math.PI));
        }
    }
    In this context "DOWN" means "towards zero". Notice that pi is actually closer to 3.142 than it is to 3.141.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Java Calculate Future Investment Amount

    Don't start a new thread for what is essentially a follow-on question, and moreover one which you already asked in the first thread. I've merged the two threads.

    Please go through the following:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 05-08-2012, 01:03 PM
  2. Replies: 0
    Last Post: 12-28-2010, 05:08 PM
  3. Future of Java
    By Skynet.Boy in forum New To Java
    Replies: 0
    Last Post: 03-06-2010, 06:16 PM
  4. Java is 15 years old - what is the Future?
    By mikeVB in forum Jobs Discussion
    Replies: 1
    Last Post: 03-04-2010, 04:21 AM
  5. Future of Java ??
    By DevzAbhi in forum Java Software
    Replies: 16
    Last Post: 02-26-2009, 10:34 PM

Tags for this Thread

Posting Permissions

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