【问题标题】:Validating input and preventing try/catch from exiting验证输入并防止 try/catch 退出
【发布时间】:2018-02-10 13:55:41
【问题描述】:

我是 Java 新手,但我已经对这个特定程序进行了很多实验,但我碰壁了。该程序的目的是捕捉用户输入的值是否超过变量限制,并验证输入是否满足我指定的范围。

  • 如果输入超过变量限制而没有 while 循环,则程序退出。
  • 如果输入超出了 while 循环的变量限制,则会无限循环。

我尝试过使用 while 循环,我的三个结果是这样的:

  1. 用户输入一个程序员指定的有效数字,一切正常。
  2. 用户输入一个大于 100 的数字,系统会提示您重试...
  3. 用户超出变量限制,程序进入 无限循环。

如果我在 catch 块中放置了一个转义符,它的作用就好像 while 不存在一样。我希望程序打印错误但允许用户重试一个值。

while(!success){
    try{
        sh = sc.nextShort();
        while(sh < 1 || sh > 100){
            System.out.print("You must enter a value between 1-100: ");
            sh = sc.nextShort();
        } //close try
        System.out.println("Great job!");
        success = true; //escape the while
    }catch{Exception ex){
        System.out.println("ERROR: " + ex);
        success = false; //causes infinite loop... I understand
    } //close catch
} //close while

【问题讨论】:

  • 我不确定我是否理解您的问题,但我怀疑您的问题是 try 块中的第一个 `sh = sc.nextShort();`。如果这就是你的所有代码,我作为用户就不会知道我必须输入一些东西,所以我会等待程序打印一些东西,然后程序会等待我的输入——典型的死锁。我怀疑发生错误时也会发生同样的情况,即它打印错误然后再次启动外部循环的主体,该主体从等待输入数字开始。一个简单的“修复”是在打印错误后打印“再试一次:”之类的内容。
  • 问题是,当抛出异常时,它会无限循环错误消息。我按照下面的帖子创建了一个新的“再试一次:”提示和扫描仪。它很实用,但我仍然需要学习很多才能准确了解正在发生的事情。感谢您的建议!

标签: java validation exception input while-loop


【解决方案1】:

据我了解您的问题,您在传递更大的值(更大的值,例如例如“10000000000”)时面临异常循环。

您在无限循环中遇到如下异常:

ERROR: java.util.InputMismatchException: For input string: "10000000000"
ERROR: java.util.InputMismatchException: For input string: "10000000000"

但您希望程序打印异常行,然后允许用户再次输入。

您可以通过以下方式从您使用的扫描仪读取错误输入 (String bad_input = sc.next();)

 while (!success) {
                  try {
                        sh = sc.nextShort();
                        while (sh < 1 || sh > 100) {
                              System.out.print("You must enter a value between 1-100: ");
                              sh = sc.nextShort();
                        } // close try
                        System.out.println("Great job!");
                        success = true; // escape the while
                  } catch (Exception ex) {
                        System.out.println("ERROR: " + ex);
                        success = false; // causes infinite loop... I understand
                        String bad_input = sc.next();// your bad input to allow read that exception
                  } // close catch
            } // close while

这个问题的原因可以找到here:

如果翻译成功,扫描仪会前进超过输入 匹配的

【讨论】:

    【解决方案2】:

    问题是,Scanner 存储整个输入字符串并对其进行处理。 当您的输入不是短值而是字符串时,它会引发异常。您将异常记录在 catch 中,然后在下一个循环中,它会尝试从同一个存储的字符串中读取一个短值,这会再次引发异常 .... -> 无限循环。

    如果你在 catch 中创建了一个新的 Scanner,它会再次从控制台读取:

    while (!success) {
            try {
                sh = sc.nextShort();
                while (sh < 1 || sh > 100) {
                    System.out.print("You must enter a value between 1-100: ");
                    sh = sc.nextShort();
                } //close try
                System.out.println("Great job!");
                success = true; //escape the while
            } catch (Exception ex) {
                System.out.println("ERROR: " + ex);
                sc = new Scanner(System.in);
            } //close catch
        }
    

    【讨论】:

    • 我对此进行了测试,它成功了!我仍然不太了解,但我已将其标记为正确,我会做更多的学习。谢谢!
    【解决方案3】:

    如果我正确理解了您的问题,您可以摆脱第二个 while 循环并将其替换为 if。然后您可以打印出该值必须为 1-100 并抛出异常,以使您的程序通过 catch 语句并打印出您抛出的错误。

    while(!success){
        try{
            sh = sc.nextShort();
            if(sh < 1 || sh > 100){
                System.out.print("You must enter a value between 1-100");
                throw Exception;
            } //close try
            System.out.println("Great job!");
            success = true; //escape the while
        }catch{Exception ex){
            System.out.println("ERROR: " + ex);
            success = false; //causes infinite loop... I understand
        } //close catch
    } //close while
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多