使用扫描仪进行输入时无法处理无限循环



我有这段代码:

import java.util.*;
class HelloWorld {
    public static void main(String args[]) {
        Scanner ob = new Scanner(System.in);
        int t = ob.nextInt(); //no. of Test Cases
                ob.next(); // for next input whether exception occurs or not
        int a = 0, c = 0;
        for (int j = 0; j < t; j++)
        {
            a = 0; c = 0;
            String str = ob.nextLine();
            String [] spstr = str.split("\s+");
            try
            {
                for (int i=0 ; i<spstr.length ; i++)
                {
                    if(spstr[i].equals("")) {i--;}
                    else {
                        c = c + Integer.parseInt(spstr[i]);
                        }
                }
                System.out.println(c);
            } catch (Exception e) {
                System.out.println("Invalid Input");
                }
        }
    }
}

此代码的作用是在一行中添加任何整数。在执行此操作之前,我有一个测试用例int t。这决定了必须采用多少输入。但是即使我输入整数值,这也会导致无限循环。

我看过这篇文章:如何处理由使用扫描仪的无效输入引起的无限循环,其中有很多关于如何摆脱此问题的答案。我已经遵循了答案,但我还没有解决这个问题。

注意:当我使用int t=5;时,它工作正常。但在这种情况下,如果异常被捕获两次,也会发生同样的事情。

请告诉我如何解决这个无限循环错误?

提前致谢:)

只需使用ob.nextLine()即可忽略它。我为您修复了代码,它正常工作。您的代码有几个我提到的问题。

import java.util.*;
class HelloWorld {
    public static void main(String args[]) {
        Scanner ob = new Scanner(System.in);
        int t = ob.nextInt(); 
        ob.nextLine();
        int a = 0, c = 0;
        for (int j = 0; j < t; j++)
        {
            a = 0; c = 0;
            String str = ob.nextLine();
            if(str.trim().length()>0){
            String [] spstr = str.trim().split("\s+");
            try
            {
                for (int i=0 ; i<spstr.length ; i++)
                {
                    c = c + Integer.parseInt(spstr[i]);
                }
                System.out.println(c);
            } catch (NumberFormatException e) {
                System.out.println("Invalid Input");
            }
        }
    }
  }
}
  1. 事实上,if(spstr[i].equals("")) {i--;}是毫无意义和错误的逻辑,这会让你的程序陷入无限循环。只需修剪字符串并像我所做的那样检查它是否为空。
  2. 不要简单地抓住Exception超类。这对调试不利。这里提出的Exception是你应该抓住的NumberFormatException

首先,正确的缩进有助于使代码更易于阅读。

class HelloWorld {
    public static void main(String args[]) {
        Scanner ob = new Scanner(System.in);
        int t = ob.nextInt(); //no. of Test Cases
        ob.next(); // for next input whether exception occurs or not
        int a = 0, c = 0;
        for (int j = 0; j < t; j++) {
            a = 0;
            c = 0;
            String str = ob.nextLine();
            String[] spstr = str.split("\s+");
            try {
                for (int i = 0; i < spstr.length; i++) {
                    if (spstr[i].equals("")) {
                        i--;
                    } else {
                        c = c + Integer.parseInt(spstr[i]);
                    }
                }
                System.out.println(c);
            } catch (Exception e) {
                System.out.println("Invalid Input");
            }
        }
    }
}

有几个问题。

int t = ob.nextInt(); //no. of Test Cases
ob.next(); // for next input whether exception occurs or not

我不知道你希望在这里完成什么。 这不是链接答案作为解决方案的内容。 链接的答案是指输入无效的情况,特别是引用捕获异常的情况,如下所示:

try {
    int x = ob.nextInt();
} catch (InputMismatchException e) {
    ob.next();
}

我严重怀疑与您的问题有任何关系,除非您故意输入错误数据。

然后是这个,最有可能的罪魁祸首,考虑到乍一看这是一个潜在的无限循环。

for (int i = 0; i < spstr.length; i++) {
    if (spstr[i].equals("")) {
        i--;
     } else {
         c = c + Integer.parseInt(spstr[i]);
     }
}

如果 i 为 5 并且 spstr[i].equals("") 返回 true,则i变为 4,则跳过 else 并将 i 无限递增回 5。

最新更新