Checking Errors in Main Method

Page 1 of 1

1 Replies - 937 Views - Last Post: 19 April 2010 - 06:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=169065&amp;s=38bc8cdca5b88011bc8fdbf312480693&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 pscmh7

• New D.I.C Head

Reputation: 0
• 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.in);
System.out.println("Please enter your initial amount deposited:");
initialDeposit=keyboard.nextDouble();
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;
}
else
{
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\Savings.java:14: errorsCheck(double,double,double) in Savings cannot be applied to (double)
Savings.errorsCheck(initialDeposit);
^
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

• D.I.C Regular

Reputation: 65
• 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
```errorsCheck(double)
```

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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }