Java Reference
In-Depth Information
Line 18: Set the method return type to void (recalculate method doesn't return any value).
Line 18: Add the parameter data types to double fundAmount, double rate (this also
solves the bug indicated at line 10).
Line 13: Add a semicolon at the end.
Line 14: Change to println ( printline is not correct syntax).
Line 15: Change the square bracket to a parenthesis.
Line 22: Delete the last closing curly bracket (there was one too many).
After all of these corrections, the syntax errors are resolved and your program can be executed. The
resulting code looks like this:
public class Errors {
public static void main(String[] args) {
int age = 30;
int retirementFund = 10000;
int yearsInRetirement = 0;
String name = "David Johnson";
for (int i = age; i <= 65; i++){
recalculate(retirementFund,0.1);
}
int monthlyPension = retirementFund/yearsInRetirement/12;
System.out.println(name + " will have $" + monthlyPension
+ " per month for retirement.");
}
public static void recalculate(double fundAmount, double rate){
fundAmount = fundAmount*(1+rate);
}
}
identifying runtime errors
If you tried running the program after fixing all the syntax errors, you probably already found your
first runtime error. In the console you'll find red text indicating there was an exception in the thread
main . See Figure 6-5. Exception is a Java class including many types of runtime problems. Java
distinguishes exceptions and errors: exceptions can—and should—be managed by the programmer,
while errors are serious problems that reasonable programs are not expected to handle. Here, the
word error is more generally applied.
figure 6-5  
 
Search WWH ::




Custom Search