Jeremy B
Jeremy B

Reputation: 863

BigDecimal Class Java

I'm trying to write a program for school using the BigDecimal class. The program is an interest rate calculator and and the final output should be somethig like:

    Loan Amount: whatever loan amount is in dollars
    Interest Rate: as a percent
    Interest: amount of interest paid in dollars
    continue? Y/N: 

The book isn't clear on how to code the BigDecimal class and I'm using Eclipse so any time I make a change I get an error which is confusing. Could someone look this over and set me in the right direction? I'm using Murach's Java SE6 and the book just isn't very helpful.

Thank you!

    import java.util.Scanner;           //import scanner
    import java.text.NumberFormat;      //import number format
    import java.math.*;                 //import math classes


    public class InterestCalculator     //create public class
    {

        public static void main(String[] args) 
            {
            Scanner calc = new Scanner(System.in);  //create scanner
            double LoanAmount, InterestRate, Interest; //declareLoanAmount,InterestRate, and Interest as double

            //welcome user to the Interest Rate Calculator
            System.out.println("Welcome to The Interest Rate Calculator");
            System.out.println();
            //perform choice calculations until choice isn't equal to "y" or "Y"
            String choice = "y";    
            while (choice.equalsIgnoreCase("y"))
                {

                //Get Loan Amount from user
                System.out.println("Enter Loan Amount: ");
                LoanAmount = calc.nextDouble();

                //Get Interest rate from user
                System.out.println("Enter Interest Rate: ");
                InterestRate = calc.nextDouble();

                BigDecimal decimalInterest = new BigDecimal(Double.toString(Interest));
                decimalInterest = decimalInterest.setScale(2, RoundingMode.HALF_UP);
                BigDecimal decimalInterestRate = new BigDecimal(Double.toString(InterestRate));
                decimalInterestRate = decimalInterestRate.setScale(2, RoundingMode.HALF_UP);

                //calculate interest



                System.out.println("message");


                //prompt user to continue?
                System.out.println("Continue? Y/N: ");
                choice = calc.next();
                System.out.println();



                }


        }

    }

Upvotes: 1

Views: 1938

Answers (1)

fyr
fyr

Reputation: 20869

Your problem is related to this point

BigDecimal decimalInterest = new BigDecimal(Double.toString(Interest));

The variable Interest is not initalized at this point.

Something like this should do the job(however i did not improve your coding style):

        BigDecimal decimalInterestRate = new BigDecimal(Double.toString(InterestRate));
        decimalInterestRate = decimalInterestRate.setScale(2, RoundingMode.HALF_UP);
        BigDecimal decimalLoanAmount = new BigDecimal(Double.toString(LoanAmount));
        decimalLoanAmount = decimalLoanAmount.setScale(2, RoundingMode.HALF_UP);

        // calculate interest
        BigDecimal Interest = decimalInterestRate.multiply(decimalLoanAmount);

        System.out.println("Interest:" + Interest);

P.S. you need to remove the Interest declaration at the very beginning of your main method.

Upvotes: 2

Related Questions