What error do you find in this code/program? Erro

2019-05-26 13:29发布

So my question is on this: This program is supposed to calculate the area of circle the user inputs a number comes out with the answer also any invalid input is not allowed i use the try-catch but it wont work...

Thank you so much for you time everyone :))

Here is the code:

import java.util.Scanner; 
import java.io;

/**
 *
 * @author Osugule
 */
public class AreaCircle { 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r); 
try {

}
catch( NumberFormatException e ) {
    System.out.println("Invalid Input, please enter a number");
    //put a message or anything you want to tell the user that their input was weird.
}

String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle  is " + area);

    }
}

3条回答
何必那么认真
2楼-- · 2019-05-26 14:02

I edited the answer so it will ask the user again if the input is wrong

You need to put nextDouble in the try clause:

boolean inputProcessed = false;
while (!inputProcessed) {
    try {
        double r = sc.nextDouble(); // Read in the double from the keyboard
        double area = (3.14 *r * r); 
        String output = "Radius: " + r + "\n";
        output = output + "Area: " + area + "\n";
        System.out.println("The area of the circle  is " + area);
        // this indicates that we were able to process the input, and we should stop now.
        inputProcessed = true; 
    }
    catch( NumberFormatException e ) {
        System.out.println("Invalid Input, please enter a number");
        //put a message or anything you want to tell the user that their input was weird.
    }
}

Note that I added the line after the try-catch to the try clause, as if you fail to parse the number, they should not be printed.

查看更多
狗以群分
3楼-- · 2019-05-26 14:05

You should put these two lines -

double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r); 

inside the try block -

import java.util.Scanner; 
import java.io;

/**
*
* @author Osugule
*/
public class AreaCircle
{ 
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in); // read the keyboard
        System.out.println("This program will calculate the area of a circle");            
        boolean correct_input = false;

        while(correct_input == false)
        {
            System.out.println("Enter radius:");//Print to screen
            try
            {
                double r = sc.nextDouble(); // Read in the double from the keyboard
                double area = (3.14 *r * r); 
                String output = "Radius: " + r + "\n";
                output = output + "Area: " + area + "\n";
                System.out.println("The area of the circle  is " + area);
                correct_input = true;
            }
            catch( NumberFormatException e )
            {
                System.out.println("Invalid Input, please enter a number");
                //put a message or anything you want to tell the user that their input was weird.
            }
            catch( InputMismatchException e )
            {
                System.out.println("Input Mismatch, please enter a number");
                //put a message or anything you want to tell the user that there is an 
                //input mismatch.
            }
        }

    }
}

From the documentation -

public double nextDouble()

Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched.

Throws:

InputMismatchException - if the next token does not match the Float regular expression, or is out of range.

NoSuchElementException - if the input is exhausted

IllegalStateException - if this scanner is closed

So if you enter an invalid character, sc.nextDouble() is probably throwing an InputMismatchException which won't be caught by the catch block because it only catcher NumberFormatException. So your program will terminate after throwing an exception but you won't get any messages.

To handle this scenario also, add the following block, just under your current catch block -

catch( NumberFormatException e )
{
    System.out.println("Invalid Input, please enter a number");
    //put a message or anything you want to tell the user that their input was weird.
}

// New catch block 
catch( InputMismatchException e )
{
    System.out.println("Input Mismatch, please enter a number");
    //put a message or anything you want to tell the user that there is an 
    //input mismatch.
}
查看更多
萌系小妹纸
4楼-- · 2019-05-26 14:19

You must catch InputMismatchException exception like this:

import java.util.InputMismatchException;
import java.util.Scanner; 
public class AreaCircle { 
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in); // read the keyboard
        System.out.println("This program will calculate the area of a circle");
        System.out.println("Enter radius:");//Print to screen
        try {
            double r = sc.nextDouble(); // Read in the double from the keyboard
            double area = (3.14 *r * r);

            String output = "Radius: " + r + "\n";
            output = output + "Area: " + area + "\n";
            System.out.println("The area of the circle  is " + area);

        }
        catch( InputMismatchException e ) {
            System.out.println("Invalid Input, please enter a number");
            //put a message or anything you want to tell the user that their input was weird.
        }
        catch( NumberFormatException e ) {
            System.out.println("Invalid Input, please enter a number");
            //put a message or anything you want to tell the user that their input was weird.
        }
    }
}
查看更多
登录 后发表回答