Results 1 to 6 of 6
  1. #1
    dave12har is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default 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!");
    inputScanner.next(); // discard the bad input
    }
    } while (years<0 || years>500000);

    }
    catch (InputMismatchException e) {
    System.out.println("Must enter an integer!");
    inputScanner.next(); // discard the bad input
    }
    } 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. #2
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    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. #3
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    who's going to take a loan for 100,000 years :P

  4. #4
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    That one's easy - the US Government!

  5. #5
    dave12har is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default 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. #6
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    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
    }

Similar Threads

  1. While loop problem
    By mochibon in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 08:21 PM
  2. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  3. Problem with loop
    By Tykk in forum New To Java
    Replies: 7
    Last Post: 10-04-2009, 10:26 PM
  4. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 09:30 AM
  5. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •