Taylor Redeemer
Taylor Redeemer

Reputation: 31

What to put into try block?

What I'm supposed to do is whenever someone puts in a string instead of an integer, such as "two", it should say "You must enter positive numeric data!" instead of a bunch of error messages. I think I have the catch statement figured out, but I'm not sure that to put into try. Here is what I have so far:

package loanpaymentfinder;

import java.text.NumberFormat;
import java.util.Scanner;


public class LoanPaymentFinder {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        NumberFormat num = NumberFormat.getCurrencyInstance();

        double numInputLoan;
        double numInputRate;
        double numInputYears;
        double numMonths;
        double numRateSquared;
        double numOutput;
        double numRatePlusOne;
        boolean finish = false;

        while (true) {

            //Input
            System.out.print("Enter loan amount: ");
            numInputLoan = Double.parseDouble(in.nextLine());
            try {

            } catch (NumberFormatException exception) {
                System.out.println("You must enter positive numeric data!");
                System.out.print("Would you like to calculate again (y/n):");
                String input = in.nextLine();
                if (input.equals("y")) {
                    continue;
                } else if (input.equals("n")) {
                    break;
                }
            }
            System.out.print("Enter rate: ");
            numInputRate = Double.parseDouble(in.nextLine());

            System.out.print("Enter number of years: ");
            numInputYears = Double.parseDouble(in.nextLine());

            //Checks for negitive inputs

            if (numInputLoan <= 0) {
                System.out.println("You must enter positive numeric data!");
                System.out.print("Would you like to calculate again (y/n):");
                String input = in.nextLine();
                if (input.equals("y")) {
                    continue;
                } else if (input.equals("n")) {
                    break;
                }

            } else if (numInputRate <= 0) {
                System.out.println("You must enter positive numeric data!");
                System.out.print("Would you like to calculate again (y/n):");
                String input = in.nextLine();
                if (input.equals("y")) {
                    continue;
                } else if (input.equals("n")) {
                    break;
                }
            } else if (numInputYears <= 0) {
                System.out.println("You must enter positive numeric data!");
                System.out.print("Would you like to calculate again (y/n):");
                String input = in.nextLine();
                if (input.equals("y")) {
                    continue;
                } else if (input.equals("n")) {
                    break;
                }
            } else {
                //Math goes here
                numMonths = 12 * numInputYears;
                numInputRate = numInputRate / 1200;
                numRatePlusOne = numInputRate + 1;
                numRateSquared = Math.pow(numRatePlusOne, numMonths);
                numOutput = numRateSquared / (numRateSquared - 1);
                numOutput = numInputRate * numInputLoan * numOutput;

                //Output
                System.out.println("The mounthly payment is:");
                System.out.println(num.format(numOutput));
                System.out.print("Would you like to calculate again (y/n):");
                String input = in.nextLine();
                if (input.equals("y")) {
                    continue;
                } else if (input.equals("n")) {
                    break;
                }
            }
        }
    }
}

Upvotes: 1

Views: 209

Answers (6)

user2380133
user2380133

Reputation:

A try statement is used to catch exceptions that might be thrown as your program executes. You should use a try statement whenever you use a statement that might throw an exception That way, your program won’t crash if the exception occurs.

In your code,this numInputLoan = Double.parseDouble (in.nextLine()); might throw exception so to avoid crashing your program you should keep this in try so that if excpetion occurs then control goes to catch and prevent from crashing.use this way

try
{
numInputLoan = Double.parseDouble (in.nextLine());
}
catch(Exception e)
{
}

Upvotes: 1

Mickey
Mickey

Reputation: 933

This should do it ;)

    try
    {
       numInputLoan = Double.parseDouble (in.nextLine());
    }
    catch(...)
    ...

Upvotes: 1

codingenious
codingenious

Reputation: 8653

Put numInputLoan = Double.parseDouble (in.nextLine()); in try block.

Your code must be

// Input
    System.out.print("Enter loan amount: ");

    try
    {
        numInputLoan = Double.parseDouble(in.nextLine());
    }

in.nextLine() returns what you have entered in console, so when you parse it to Double, if it has string, it will give NumberFormatException, so this part goes in try.

and initialize numInputLoan also, otherwise you will get compilation error.

Upvotes: 1

Wim Ombelets
Wim Ombelets

Reputation: 5265

What you'll have to put inside the try block is the actual attempt at converting the input (String) into an integer. Contrary to some languages like C#, Java doesn't have an Int.TryParse(String s) method, so the result and subsequent handling of your parsing should be enclosed in a try so you can deal with faulty input.

That's why

numInputLoan = Double.parseDouble (in.nextLine());

should be in the try block, so that when it fails and throws an Exception, the catch block can take over.

Upvotes: 1

Robin Dijkhof
Robin Dijkhof

Reputation: 19278

In the try block you put some code that could go wrong. Something you want to try.

Look at this part code you added:

numInputLoan = Double.parseDouble (in.nextLine());

You want to parse a String to a double, but what if this string contains a 's' for example? It gives an error because you can't parse a 's' to a double. So you should TRY to execute it and you might CATCH an error.

conclusion, move numInputLoan = Double.parseDouble (in.nextLine()); to the try block.

Upvotes: 3

kviiri
kviiri

Reputation: 3302

In the try block, you put anything that could cause the exception you're checking for. In this case, since you're preparing for the NumberFormatException, you probably want to enclose at least the statement where you call parseDouble. Like this:

try {
    numInputLoan = Double.parseDouble (in.nextLine());
}

Upvotes: 0

Related Questions