Thread: Major Java Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    9
    Rep Power
    0

    Major Java Help


    Still having a bit of trouble with my code.

    Code:
    public static void InvestmentReport()
        {
            System.out.printf("*** Investment Report stub ***\n");
            // This is where your Part A solution goes
            System.out.printf("*************** InvestmentReport Menu ***************\n\n");
            Scanner console=new Scanner(System.in);
            double deposit = 0D;
            double interest = 0D;
            double monthly = 0D;
            double interestRate = 0D;
            System.out.println ("Enter your initial deposit amount in dollars\n");
            deposit = console.nextDouble();
             
            System.out.println ("Enter the annual interest rate as a percentage (eg. 6.0)\n");
            interestRate = console.nextDouble();
           
            System.out.println ("Enter your monthly deposit amount in dollars\n");
            monthly = console.nextDouble();
           
            System.out.println ("Savings growth over the next 6 months:\n");
           
            deposit = deposit + monthly;
            interest = deposit * ((interestRate / 12) / 100);
            deposit = deposit + interest;
            System.out.println ("Balance after first month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after second month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after third month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after fourth month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after fifth month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after sixth month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
        }
    This is the data it should be displaying,

    Enter your initial deposit amount in dollars: 1000
    Enter the annual interest rate as a percentage (eg. 6.0): 6.0
    Enter your monthly deposit amount in dollars: 500
    Savings growth over the next 6 months:
    Balance after first month: $1507.50
    Interest earned for this month: $7.50
    Balance after second month: $2017.54
    Interest earned for this month: $10.04
    Balance after third month: $2530.13
    Interest earned for this month: $12.59
    Balance after fourth month: $3045.28
    Interest earned for this month: $15.15
    Balance after fifth month: $3563.00
    Interest earned for this month: $17.73
    Balance after sixth month: $4083.32
    Interest earned for this month: $20.32

    But instead it displays it like this,

    1000
    Enter the annual interest rate as a percentage (eg. 6.0)

    6.0
    Enter your monthly deposit amount in dollars

    500
    Savings growth over the next 6 months:

    Balance after first month: $1507.5
    Interest earned for this month: $7.5
    Balance after second month: $2017.5375
    Interest earned for this month: $10.0375
    Balance after third month: $2530.1251875
    Interest earned for this month: $12.5876875
    Balance after fourth month: $3045.2758134375
    Interest earned for this month: $15.1506259375
    Balance after fifth month: $3563.002192504687
    Interest earned for this month: $17.726379067187498
    Balance after sixth month: $4083.3172034672107
    Interest earned for this month: $20.315010962523434

    As you can see it has too many decimal points and i need it to round off to the nearest number. How would i go about doing this? Please help? Thankyou.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Look at the DecimalFormat class for a way to control the number of decimal places shown.

IMN logo majestic logo threadwatch logo seochat tools logo