# Problem with For Loop

• 11-30-2010, 12:28 AM
dave12har
Problem with For Loop
Hi All,

I'm relatively new to Java.

I am trying to develop a program for a loan calculation. Basically the user will enter a loan amount and the number of years for repayment using the scanner class.

Then the user is shown a list of interest rates ranging from 3% - 10% and the associated monthly repayments.

I had it working perfectly but then I added try catch blocks for input validation and now the for loop is not terminating. I have spent the past hour trying a number of ways to rectify it but I've had no success.

This is the code:

import java.util.*; // needed for Scanner
public class InputValidation {
public static void main(String[] args) {
int amount=-1;
int years=-1;
double result;
int rate;
double totalAmount;
double monthly;

Scanner inputScanner = new Scanner(System.in);
do {
System.out.println("Enter loan amount : ");
try {
amount = inputScanner.nextInt();
do {
System.out.println("Enter Number of Years : ");
try {

years = inputScanner.nextInt();

}
catch (InputMismatchException e) {
System.out.println("Must enter an integer YEAR!");
}
} while (years<0 || years>500000);

}
catch (InputMismatchException e) {
System.out.println("Must enter an integer!");
}
} while (amount<0 || amount>100000);

for(rate=3; rate<9.25; rate+=.25){
totalAmount = (amount*(rate/100)+amount);

monthly = (totalAmount/years)/12;

System.out.print("Interest Rate = " + rate+"\t");
System.out.print(" Monthly Payment = " + monthly+"\t");
System.out.println("Total Amount = " + totalAmount+"\t");

}

}

}

Hopefully you can help!

Thanks alot,

Dave
• 11-30-2010, 12:41 AM
Dave,

First suggestion - if you use CODE and /CODE to mark blocks of code (use the # option in the reply editor) this preserves your code formatting and makes it easier to read.

Second suggestion - do not nest your do-while loops inside try-catch blocks. On each try block, immediately follow it with a catch block to handle any exceptions (before starting another do-while loop). This should help, or at least make the code more readable and easier to debug.

I hope this helps.
• 11-30-2010, 12:55 AM
maknib
who's going to take a loan for 100,000 years :P
• 11-30-2010, 01:16 AM
That one's easy - the US Government!
• 11-30-2010, 04:28 PM
dave12har
Try Catch Problem
Hi again,

I have taken the try/catch out of the do/while loops.

My question now is how do I get the program to keep asking the user to enter an integer value if they enter a string value.

Code:

```System.out.println("Please enter loan amount: ");                 try {                         inputAmount=myScanner.nextLine();                         amount = Integer.parseInt(inputAmount);                         }                 catch (RuntimeException e) {                         System.out.println("You must enter an integer value!");                         }```
The way the code is now it will just tell the user they must enter an integer value and then ends.

Thanks,

Dave
• 11-30-2010, 04:35 PM
```System.out.println("Please enter loan amount: "); try {         inputAmount=myScanner.nextLine();         amount = Integer.parseInt(inputAmount); } catch (RuntimeException e) {         System.out.println("You must enter an integer value!");             amount = -1;  // Set invalid value so do-while loop doesn't exit }```