2

So I keep receiving the answer of 0 regardless of the inputs to the method. When I input 6 months, 100 savings amount, and 0.05 interest rate it should give me 608.81 but the response is 0. I feel like it could be my for loop or the parameters that I have set that are messing with it. I have tried all I can think of regarding the for loop. Here's the code:

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class FifthAssignment2 {

    static double savingAmount = 0.0; // the 2 given static doubles
    static double annualInterestRate = 0.0;
    static int numberOfMonth;
    static double sixthMonth = 0.0;;

    public static double compoundValueMethod(double savingAmount, double annualInterestRate, int NumberOfMonth) {

        {
        DecimalFormat formatter = new DecimalFormat(".00");

        double monthlyInterestRate = annualInterestRate / 12;

        if (savingAmount < 0)
            if (annualInterestRate < 0) {
                JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
                System.exit(0);
            }

        if (savingAmount < 0) {
            JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
        }

        else if (annualInterestRate < 0) {
            JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
        }

        else {
            for (int i = 0; i < numberOfMonth; i++) {
                sixthMonth = ((savingAmount+sixthMonth) * (1 + monthlyInterestRate));
            }
        }
        return sixthMonth;
        }
    }

    public static void main(String[] args) {

        DecimalFormat formatter = new DecimalFormat(".00");

        int numberOfMonth;                                                  


        String NOM = JOptionPane.showInputDialog("How many months? ");
        numberOfMonth = Integer.parseInt(NOM);

        String SA = JOptionPane.showInputDialog("What is your savings amount? "); 

        savingAmount = Double.parseDouble(SA); 

        String AIR = JOptionPane.showInputDialog("What is the annual interest rate? "); // Window pops up
        annualInterestRate = Double.parseDouble(AIR); 

        {
            JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is "
                    + compoundValueMethod(savingAmount, annualInterestRate, numberOfMonth));
        }

    }
}
nbrooks
  • 18,126
  • 5
  • 54
  • 66
josh102894
  • 35
  • 4

3 Answers3

3

You are passing the following variables to your method :

public static double compoundValueMethod(double savingAmount,
                                         double annualInterestRate,
                                         int NumberOfMonth)

Note but use the static variable numberOfMonth in your method instead of the passed NumberOfMonth parameter (variable names in Java are case sensitive). numberOfMonth is initialized to 0 by default, so your for loop is not entered and the method returns 0.

You should eliminate the static variables and only use local variables. Had you done that in the first place, you'd get a compilation error which would make it much easier to find your bug.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Wow. This whole time for one capitalized letter. Thank you so much!! – josh102894 Feb 25 '16 at 06:28
  • if you don't mind me asking how would I format it to where it only uses two decimal places. previously I was using formatter.format – josh102894 Feb 25 '16 at 06:34
  • 1
    @josh102894 check [this question](http://stackoverflow.com/questions/12806278/double-decimal-formatting-in-java). – Eran Feb 25 '16 at 06:36
1

You dont need static variables. Update code as below

public class FifthAssignment2 {

public static double compoundValueMethod(double savingAmount, double annualInterestRate, int numberOfMonth) {
    {
        DecimalFormat formatter = new DecimalFormat(".00");
        double sixthMonth = 0.0;
        double monthlyInterestRate = annualInterestRate / 12;

        if (savingAmount < 0)
            if (annualInterestRate < 0) {
                JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
                System.exit(0);
            }

        if (savingAmount < 0) {
            JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
        }

        else if (annualInterestRate < 0) {
            JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
        }

        else {
            for (int i = 0; i < numberOfMonth; i++) {
                sixthMonth = ((savingAmount+sixthMonth) * (1 + monthlyInterestRate));
            }
        }
        return sixthMonth;
    }
}




public static void main(String[] args) {

    DecimalFormat formatter = new DecimalFormat(".00");

    int numberOfMonth;                                                  


    String NOM = JOptionPane.showInputDialog("How many months? ");
    numberOfMonth = Integer.parseInt(NOM);


    double savingAmount; 

    String SA = JOptionPane.showInputDialog("What is your savings amount? "); 

    savingAmount = Double.parseDouble(SA); 






    String AIR = JOptionPane.showInputDialog("What is the annual interest rate? "); // Window
    // pops
    // up

    double annualInterestRate;
    annualInterestRate = Double.parseDouble(AIR); 

    {
        JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is "
                + compoundValueMethod(savingAmount, annualInterestRate, numberOfMonth));
    }

}

}

sanky jain
  • 873
  • 1
  • 6
  • 14
0

use NumberOfMonth instead of numberOfMonth

for (int i = 0; i < NumberOfMonth; i++) {
                sixthMonth = ((savingAmount+sixthMonth) * (1 + monthlyInterestRate));
            }

or inside method you can assign

numberOfMonth=NumberOfMonth;
Musaddique S
  • 1,539
  • 2
  • 15
  • 36