
-----------------------------------
samanthaxc
Thu Mar 07, 2013 9:23 pm

Java Method Help
-----------------------------------
My program is supposed to allow the user to input a principle amount and rate, which it will calculate the the balance after it has been compounded for 10 years. It should display the balance at the end of every year. My problem is that when I try to return the amount to the main method, it won't work. some please help!


import java.io.*;
import java.text.*;

public class compoundInterest {
public static void main( String

Mod Edit:

Please wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
 ... code ... 

[code] ... code ... [/code ]
[/code]

-----------------------------------
Zren
Thu Mar 07, 2013 9:45 pm

RE:Java Method Help
-----------------------------------
Define exactly how it doesn't work. Does it show the wrong calculation? Does it not output at all? Does it cause an error? If it causes an error, what does the error message say?

-----------------------------------
samanthaxc
Thu Mar 07, 2013 9:47 pm

Re: Java Method Help
-----------------------------------
When I compile the program, it says the "return finalNumB" is incompatible and should be an 'int'

-----------------------------------
Zren
Thu Mar 07, 2013 9:52 pm

RE:Java Method Help
-----------------------------------
Well you're defining it as an int alright. However, you seem to be trying to return a value that hasn't been initialized (you don't set it to anything).

You're probably going to run into all the years showing the same result. For this, I suggest you take a closer look at what variables you're using for what (and which you're not using at all).

You also seem to be trying to format the value into a string inside that function that will be returning an integer. I suggest you make two functions, one to format the number into a humanly readable money string, and another to do your logic.
Eg:

formatMoney(compoundInterest(deposit, rate, numIterations))


-----------------------------------
ishidon
Thu Mar 07, 2013 10:13 pm

Re: Java Method Help
-----------------------------------
I believe what you're looking for is
[code]
import java.io.*; 

public class compoundInterest { 
    public static void main( String[] args ) throws IOException { 
        String depositA, rateA;// String variables are declared 
        double deposit, rate;// double variables are declared 
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed by the user 
        
        System.out.println("Enter the amount of your initial deposit: ");// asks user to input deposit amount 
        depositA = myInput.readLine();// reads previous value entered 
        deposit = Double.parseDouble(depositA);// converts value into a double/decimal 
        
        System.out.println("Enter the rate in a decimal form (e.g: 5% -> 0.05): ");//asks user to input rate in decimal form 
        rateA = myInput.readLine();// reads previous value entered 
        rate = Double.parseDouble(rateA);// converts value into a double/decimal 
        
        double num;
        for (int year = 1; year 