# Thread: Problem with For Loop

1. Member
Join Date
Nov 2010
Posts
2
Rep Power
0

## 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

2. Member
Join Date
Nov 2010
Posts
33
Rep Power
0
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.

3. Member
Join Date
Nov 2010
Posts
90
Rep Power
0
who's going to take a loan for 100,000 years :P

4. Member
Join Date
Nov 2010
Posts
33
Rep Power
0
That one's easy - the US Government!

5. Member
Join Date
Nov 2010
Posts
2
Rep Power
0

## 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.

Java 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

6. Member
Join Date
Nov 2010
Posts
33
Rep Power
0
Dave,

One approach would be to set an invalid value in the amount variable in your catch block. Something like this:

Java 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!");
amount = -1;  // Set invalid value so do-while loop doesn't exit
}```