5

I've been staring at this for hours and unable to think of a solution; I usually handle validation of this type with regex but am trying to use a built-in solution for a change (obviously, I don't do this frequently):

private static double promptUserDecimal(){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a decimal");
    try{
        double input2 = Double.parseDouble(scan.nextLine());
        return input2;
    } catch(NumberFormatException e){
        System.out.println("Sorry, you provided an invalid option, please try again.");
    }
}

The error with this is that the "return" isn't found by the compiler so I get a compile error. If I put the "return" outside of the try/catch I need to declare/initialize "input2" which defeats the purpose of the operation. Any assistance is appreciated...

4

8 回答 8

3

Throw an exception in the catch section. Wherever you call the promptUserDecimal method, catch any exception and print the message there:

public static void main(String[] args) {

    double d = 0.0;
    while (double == 0) {
        try {
            d = promptUserDecimal();
        } catch (NumberFormatException e) {
            //log the message...
            d = 0.0;
        }
    }
}

private static double promptUserDecimal() throws NumberFormatException {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a decimal");
    return Double.parseDouble(scan.nextLine());
}

This would be a better approach because you let the promptUserDecimal cares only about handling reading a double value. You must try to tend to separate each class and method for the specific purpose it was designed for.

于 2012-10-07T22:27:41.900 回答
3

You need something like:

double input2;
try{
  //read input2
}catch( ... ){
  //... log AND assign a value to input2 in case of invalid input
}
return input2;
于 2012-10-07T22:27:42.260 回答
3

If you want the user to "please try again", it sounds like you need a loop:

private static double promptUserDecimal(){
    final Scanner scan = new Scanner(System.in);

    // Ask for input until we get something valid
    while (true) {  // Terminated by return within
        System.out.println("Enter a decimal");
        try {
            return Double.parseDouble(scan.nextLine());
        } catch(NumberFormatException e){
            System.out.println("Sorry, you provided an invalid option, please try again.");
            // No return, so the loop will run again
        }
    }
}
于 2012-10-07T22:31:59.587 回答
1

Let your method throw an exception or return nan.

于 2012-10-07T22:27:40.290 回答
0

You can throw an exception within your catch block. i.e.,

private static double promptUserDecimal() throws OopsException {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a decimal");
    try{
        double input2 = Double.parseDouble(scan.nextLine());
        return input2;
    } catch(NumberFormatException e){
        System.out.println("Sorry, you provided an invalid option, please try again.");
        throw new OopsException();
    }
}

Then, every time they give an invalid input, you can catch it and handle it where you call the method from.

于 2012-10-07T22:29:46.110 回答
0
private static double promptUserDecimal(){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a decimal");
    double input2 = 0.0; // <-- explicit initialization
    try{
        double input2 = Double.parseDouble(scan.nextLine());
    } catch(NumberFormatException e){
        System.out.println("Sorry, you provided an invalid option, please try again.");
    }
    return input2;
}
于 2012-10-07T22:30:29.857 回答
0

You need to return or throw something from (or after the catch). Judging by your output to the user, it looks like you just want to do the same thing again. Just call the method again and return the result.

private static double promptUserDecimal(){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a decimal");
    try{
        double input2 = Double.parseDouble(scan.nextLine());
        return input2;
    } catch(NumberFormatException e){
        System.out.println("Sorry, you provided an invalid option, please try again.");
        return promptUserDecimal();
    }
}
于 2012-10-07T22:48:43.243 回答
0

A few of the solutions listed will solve the compiler issue, but the first step is to take a step back and ask "what do I want to do in the event that a NumberFormatException occurs?"

One option is to propagate the exception by rethrowing the NumberFormatException or wrapping it in a RuntimeException so that it goes unchecked. This means that calling code will have to handle it, or the user will be presented with a stacktrace. If you go this route you don't even need a try catch in your method. You can just declare "throws NumberFormatException" on the method signature and let it get handled up stream.

Another option is to return null, either by using "return null" as the last statement in your catch block, or returning null as the last statement within the method. This is a terrible option because calling code and/or end user won't get the information they need, that "a non-number was entered as input."

I would go with option one, and handle the exception by telling the user that scan.nextline + " is not recognized as a valid double."

于 2012-10-08T00:01:08.353 回答