如何在Java中执行扫描程序异常?

时间:2022-09-23 07:47:16

I would like to scan a float and an integer. I want to add an exception, if the scanned number is not a float or int, scan again while the input number is not correct. I tried with hasNextFloat and hasNextInt but I didn't really got it right.

我想扫描浮点数和整数。我想添加一个例外,如果扫描的数字不是浮点数或整数,则在输入数字不正确时再次扫描。我尝试使用hasNextFloat和hasNextInt,但我没有真正做对。

package has_exception;

import java.util.Scanner;

public class Has_Exception {

    public static void main(String[] args) {

        Scanner scn = new Scanner(System.in);

        System.out.println("Enter a float!");
        float fl = scn.nextFloat();

        System.out.println("Enter an integer");
        int a = scn.nextInt();

    }
}

2 个解决方案

#1


1  

try with a parse of string into a float and do this until the user gives a valid input... the initial values of the float will depend of your application, and you can repeat this approach for the integer value

尝试将字符串解析为float并执行此操作直到用户提供有效输入... float的初始值将取决于您的应用程序,并且您可以对整数值重复此方法

    Scanner scn = new Scanner(System.in);
    float fl = -1.0f;

    while (fl < 0) {
        System.out.println("Enter a float!");

        String x = scn.nextLine();
        try {
            fl = Float.parseFloat(x);
        } catch (NumberFormatException e) {
            System.out.println("Not a float");
        }
    }

#2


1  

You can use do{..}while() with nextLine() instead, for example :

您可以使用do {..} while()代替nextLine(),例如:

Scanner scn = new Scanner(System.in);
boolean correct = true;
do {
    try {

        System.out.println("Enter a float!");
        float fl = Float.parseFloat(scn.nextLine());
        System.out.println("Enter an integer");
        int a = Integer.parseInt(scn.nextLine());

    } catch (NumberFormatException e) {
        correct = false;
    }
} while (!correct);

scn.close();//close your scanner

#1


1  

try with a parse of string into a float and do this until the user gives a valid input... the initial values of the float will depend of your application, and you can repeat this approach for the integer value

尝试将字符串解析为float并执行此操作直到用户提供有效输入... float的初始值将取决于您的应用程序,并且您可以对整数值重复此方法

    Scanner scn = new Scanner(System.in);
    float fl = -1.0f;

    while (fl < 0) {
        System.out.println("Enter a float!");

        String x = scn.nextLine();
        try {
            fl = Float.parseFloat(x);
        } catch (NumberFormatException e) {
            System.out.println("Not a float");
        }
    }

#2


1  

You can use do{..}while() with nextLine() instead, for example :

您可以使用do {..} while()代替nextLine(),例如:

Scanner scn = new Scanner(System.in);
boolean correct = true;
do {
    try {

        System.out.println("Enter a float!");
        float fl = Float.parseFloat(scn.nextLine());
        System.out.println("Enter an integer");
        int a = Integer.parseInt(scn.nextLine());

    } catch (NumberFormatException e) {
        correct = false;
    }
} while (!correct);

scn.close();//close your scanner