简体   繁体   English

如何在不使用 try 和 catch 的情况下捕获 Java 中的错误“InputMismatchException”并打印个性化消息?

[英]How to catch the error “InputMismatchException” in Java and print a personalized message , without using try and catch?

    Scanner sc = new Scanner (System.in);
    int source_radix = sc.nextInt();
    String sr = source_radix+"";
    if (!Pattern.matches("[0-9]+",sr) || source_radix <1 || source_radix >36)
    {
        System.out.print("error");
        System.exit(0);
    }
   

I want to print "error" when the input is not an integer.当输入不是 integer 时,我想打印“错误”。

The simplest way to prevent that exception being thrown from nextInt() is to call hasNextInt() first.防止从nextInt()引发异常的最简单方法是首先调用hasNextInt() In your specific example, we could do this:在您的具体示例中,我们可以这样做:

Scanner sc = new Scanner (System.in);
int radix = 0;
if (sc.hasNextInt()) {
    radix = sc.nextInt();
}
if (radix < 1 || radix > 36) {
    System.out.print("error");
    System.exit(0);
}

You could modify that to print different error messages to distinguish the cases where the user didn't enter a valid integer, and where they entered a integer that was outside of the required range.您可以对其进行修改以打印不同的错误消息,以区分用户未输入有效 integer 和输入超出所需范围的 integer 的情况。

In some situations, it may be necessary to call sc.nextLine() to discard something that isn't a valid number, or anything on the line after the number.在某些情况下,可能需要调用sc.nextLine()来丢弃不是有效数字的内容,或数字后面的任何内容。 (But it isn't necessary if the app is going to terminate on getting bad input.) (但如果应用程序因输入错误而终止,则没有必要。)

Finally, it is often a bad idea to call System.exit(0) .最后,调用System.exit(0)通常是个坏主意。 If this is in your main method, return will work just as well.如果这是在您的main方法中,则return也将起作用。 If it is not in main , then it probably should not be this code's responsibility to "pull the plug" on the application.如果它不在main中,那么在应用程序上“拔掉插头”可能不应该是这段代码的责任。 But... it is not clear cut.但是……不是很清楚。

You can check if input is number of range from 1 to 36 by only using Regex:您可以仅使用正则表达式检查输入是否为 1 到 36 范围内的数字:

Scanner sc = new Scanner(System.in);
String sr  = sc.nextLine();

if (!Pattern.matches("[1-9]|[12][0-9]|3[0-6]", sr)) {
    System.out.print("error");
    System.exit(0);
}

You may try this Java code:您可以试试这个Java代码:

   Scanner sc = new Scanner (System.in);
   String sr = sc.next(); // read a string

   // check if we have all digits in input
   boolean match = Pattern.matches("[0-9]+", sr);

   // if yes then convert to int and do comparison
   if (match) {
       int source_radix = Integer.parseInt(sr);
       if (source_radix <1 || source_radix >36) match = false;
   }

   // handle error if match is false
   if (!match) {
       System.out.print("error");
       System.exit(0);
   }

You can do something this:你可以这样做:

Scanner sc = new Scanner(file);
while(sc.hasNext()) {
    String str =sc.nextLine();
    if (!Pattern.matches("[0-9]+",str) ||( Pattern.matches("[0-9]+",str) &&     Integer.parseInt(str)) <1 )|| ( Pattern.matches("[0-9]+",str) && Integer.parseInt(str)) <1 ) >36) {
        System.out.print("error");
        System.exit(0);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM