Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果输入不是bigDecimal,如何抛出异常并再次扫描?_Java - Fatal编程技术网

Java 如果输入不是bigDecimal,如何抛出异常并再次扫描?

Java 如果输入不是bigDecimal,如何抛出异常并再次扫描?,java,Java,我想扫描bigDecimal,但是如果扫描的输入不是bigDecimal,它应该抛出一个自定义异常并再次扫描它 我正在尝试下面的代码,但无法得出结论 代码: Sacnner#hastNextBigDecimal在扫描键盘输入时没有意义。它可以在扫描文件或扫描仪上字符串对象的值时使用 您可以按如下方式进行操作: import java.math.BigDecimal; import java.util.Scanner; class WrongInputException extends Exce

我想扫描bigDecimal,但是如果扫描的输入不是bigDecimal,它应该抛出一个自定义异常并再次扫描它

我正在尝试下面的代码,但无法得出结论

代码:

Sacnner#hastNextBigDecimal
在扫描键盘输入时没有意义。它可以在扫描文件或
扫描仪
字符串
对象的值时使用

您可以按如下方式进行操作:

import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a BigDecimal: ");
        try {
            number = sc.nextBigDecimal();
            System.out.println(number);
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
运行示例:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34
另一个示例运行:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34
字符串上的
扫描仪的演示:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34
输出:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34
Sacnner#hastNextBigDecimal
在扫描键盘输入时没有意义。它可以在扫描文件或
扫描仪
字符串
对象的值时使用

您可以按如下方式进行操作:

import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a BigDecimal: ");
        try {
            number = sc.nextBigDecimal();
            System.out.println(number);
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
运行示例:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34
另一个示例运行:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34
字符串上的
扫描仪的演示:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34
输出:

Enter a BigDecimal: 1234
1234
Enter a BigDecimal: xyz
Exception in thread "main" WrongInputException: Wrong data type of input.....
    at Main.main(Main.java:19)
import java.math.BigDecimal;
import java.util.Scanner;

class WrongInputException extends Exception {
    public WrongInputException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) throws WrongInputException {
        BigDecimal number = null;
        Scanner sc = new Scanner("123 987654321 12.34");
        try {
            while (sc.hasNextBigDecimal()) {
                number = sc.nextBigDecimal();
                System.out.println(number);
            }
        } catch (Exception e) {
            throw new WrongInputException("Wrong data type of input.....");
        }
    }
}
123
987654321
12.34

问题是您没有处理抛出的异常

您应该添加一个try-catch块来在thw内部处理它,同时还应该添加一个
sc.next()以避免无休止的循环

 while (!sc.hasNextBigDecimal())
         {
            try {
             throw new WrongInputException("Wrong data type of input.....");

            }catch (WrongInputException e) {
                e.printStackTrace();
            }finally {
                sc.next();
            }
        }

请注意,try-catch没有意义,因为您有要抛出的异常

问题是您没有处理抛出的异常

您应该添加一个try-catch块来在thw内部处理它,同时还应该添加一个
sc.next()以避免无休止的循环

 while (!sc.hasNextBigDecimal())
         {
            try {
             throw new WrongInputException("Wrong data type of input.....");

            }catch (WrongInputException e) {
                e.printStackTrace();
            }finally {
                sc.next();
            }
        }

请注意,您的try-catch没有意义,因为您要抛出异常

什么不起作用?为什么要抛出异常?谁应该捕获此异常?什么不起作用?为什么要抛出异常?谁应该抓住这个例外?