Checking Errors in Main Method

Recursion & type Double

Page 1 of 1

1 Replies - 969 Views - Last Post: 19 April 2010 - 06:42 PM Rate Topic: -----

#1 pscmh7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-February 09

Checking Errors in Main Method

Posted 19 April 2010 - 06:33 PM

Okay, here was the assignment:
A savings account typically accrues savings using compound interest. If you deposit 1,000 with 10% interest rate per year, then after one year you have a total of 1,100. If you leave this money in the account for another year at 10% interest, then after two years the total will be 1210. After three years you would have 1331, and so on. Write a program that inputs the amoutn of money to deposit, an interest rate per year, and the number of years the money will accrue compound interest. Write a recrusive function that calculates the amount of money that will be in the savings account after using the input information.
To verify your function, the amount should be equal to P(1+i)^n, where P is the amount of initially saved, i is the interest rate per year,a nd n is the number of years.

I completed majority of the assignment, but now I am having trouble with error checking after each keyboard entry. I would like for my program to check the parameters of each input value after its entered, and if an error does occur, I want the user to be able to reenter the value and continue onward instead of the program exiting.
import java.util.*;

public class Savings
	public static void main(String[]args)
		double interestRate;
		double initialDeposit;
		double totalYears;

		Scanner keyboard = new Scanner (;
		System.out.println("Please enter your initial amount deposited:");
		Savings.errorsCheck(initialDeposit); //WHERE I TRIED TO CHECK FOR ERRORS

		System.out.println("Now enter your annual interest rate as a decimal(eg for 4.5%, 0.045):");
		interestRate = keyboard.nextDouble();

		System.out.println("Finally, enter the number of years you plan to accumulate interest:");
		totalYears = keyboard.nextDouble();

		double finalAmount = Savings.errorsCheck(initialDeposit, interestRate, totalYears);
		System.out.println("After " + totalYears + " number of years, your balance will be: $" + finalAmount);

	//Method name: finalAmount
	//Description: recursive method that
	private static double finalAmount(double interestRate, double initialDeposit, double totalYears)
		if (totalYears ==0)
		return initialDeposit;
		return finalAmount(initialDeposit + (initialDeposit*interestRate), interestRate, (totalYears - 1));

	public static double errorsCheck(double interestRate, double initialDeposit, double totalYears)
		if(initialDeposit < 0.0)
		System.out.println("Error: The deposit you entered is less than zero.");
		if(interestRate < 0.0)
		System.out.println("Error: The interest rate you entered is less than zero.");
		if(totalYears < 0.0)
		System.out.println("Error: The number of years you entered is less than zero.");
	return Savings.finalAmount(interestRate, initialDeposit, totalYears);

With just the one error check that I have tried to write the following error appears:

C:\Users\user\Desktop\ errorsCheck(double,double,double) in Savings cannot be applied to (double)
1 error

Any suggestions or guidance would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Checking Errors in Main Method

#2 pdkharkar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Checking Errors in Main Method

Posted 19 April 2010 - 06:42 PM

your errorsCheck method takes three parameters
errorsCheck(double a, double b, double c){
//your code

in the definition but when you are calling it, you are passing just parameter

you have to pass all the three parameters
this should solve the problem
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1