Need a little Java help, interest calculator

Bateluer

Lifer
Jun 23, 2001
27,730
8
0
It seems to output the way I want it to, but someone is embezzling from the CD. This Java course I'm taking has proven to have much more math than I would have preferred.

Here's the pastebin for code, if you prefer to glance at that.
http://pastebin.com/dPf3xVTX

Code:
 	package chapter4;

	
	import java.text.DecimalFormat; // Eclipse made me add this during attempt to correct formating.
	import java.util.Scanner;
	
	public class Exercise431 {

		public static void main(String[] args) {
			// TODO Auto-generated method stub
			{ 
			Scanner s = new Scanner(System.in); 
			
			
			// Get initial deposit amount, set to var 'calc'
			System.out.println("Please enter the initial deposit amount: "); 
			double calc = s.nextDouble(); 
			
			// Get the yield %, set to var 'percentage'
			System.out.println("Please enter the annual percentage yield: "); 
			double percentage = s.nextDouble(); 
			
			// Get the maturity period. 
			System.out.println("Please enter maturity period in months: "); 
			int months = s.nextInt(); 
			double monthly[] = new double[months]; 
			for(int i = 0; i < months; i++) 
				
			{ 
				// I think this arithmetic is broken, gets different results than textbook.
			// Internet suggests this to format to 0.00, but issues.
			// DecimalFormat df = new DecimalFormat("0.00"); double d = .4; System.out.println(df.format(d));
			monthly[i] = (calc+calc * percentage /(months*100)); 
			calc = monthly[i]; 
			} 
			
			
			System.out.println("Month" + " " + " CD Value" + "\n"); 
			for(int i = 1; i <= months; i++) 
			{ 
			System.out.println(" " +i+ "      " + monthly[i-1]); 
			} 
		} 
		} 
	}

My output ends with these.

14 10456.627999170305
15 10490.031116389877
16 10523.540938011678
17 10557.157804896993
18 10590.88205899597

And according to the text book, verified with tools on USBank, it should end like this.
17 10846.56
18 10898.54

I'm pretty sure I've screwed up my math somewhere. Also, I'm not sure how to cut my outputs off at 2 decimal places. I haven't bothered in previous exercises, but this is a financial exercise. The bank owns everything after the third decimal point. ;)

Edit - I added the "// DecimalFormat df = new DecimalFormat("0.00"); double d = .4; System.out.println(df.format(d));" line after some Googling after creating that Pastebin, but I can't get it to work, so I've commented it out for now
 

sze5003

Lifer
Aug 18, 2012
14,319
682
126
Can you try using BigDecimal class. That should format output correctly.

Also, set calc+calc to a local variable and the system out should look like

(calc+calc) * (percentage/(months*100));

I would also put months*100 into a variable for easy readability.

Also what is double d=.4 suppose to do? You are calling decimal.format on that value.
 

Bateluer

Lifer
Jun 23, 2001
27,730
8
0
Also what is double d=.4 suppose to do? You are calling decimal.format on that value.

I'm not sure exactly, that was something I'd found at Stack Overflow that one of the comments suggested for a similar formatting issue. Course hasn't officially gone over that command yet, or BigDecimal, for that matter. I muddled with it for a bit, then commented it out and moved on to the bigger problem.
 

sze5003

Lifer
Aug 18, 2012
14,319
682
126
So you can still call decimal.format and pass in a double, along with the precision to format it to. Try passing it the value you calculate at the end into the format method and then print it out.
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
Your formula for calculating the value of the CD is wrong.

Code:
// What you are trying to calculate
nextMonthsValue = principle + interest
// How you are calculating the above (I substituted variable names)
nextMonthsValue = principle + (principle*rate)/codTermInMonths * 100
// Seeing as how principle is input for you, hopefully it's clear that the
// interest calculation is incorrect. Since this looks like homework, it's up to you to
// find the correct formula

As another tip, you are on the right track with your DecimalFormat code. Get your calculation fixed first, then come back to the formatting. My preference is to only worry about formatting when displaying the result, not when calculating the result as manipulating the precision of the calculation can accidentally introduce rounding errors.
 
Last edited: