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

Java 使用扫描仪进行输入时无法处理无限循环,java,exception-handling,java.util.scanner,Java,Exception Handling,Java.util.scanner,我有一段代码: 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

我有一段代码:

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");
                }
        }
    }
}
import java.util.*;
类HelloWorld{
公共静态void main(字符串参数[]){
扫描器ob=新扫描器(System.in);
int t=ob.nextInt();//测试用例数
ob.next();//对于下一个输入,无论是否发生异常
int a=0,c=0;
对于(int j=0;j对于(inti=0;i开头,适当的缩进有助于使代码更易于阅读

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");
            }
        }
    }
}
我不知道您希望在这里实现什么。这不是链接答案的解决方案。链接答案指的是输入无效的情况,特别是指捕获异常,如下所示:

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]);
     }
}
for(int i=0;i

如果
i
为5,且
spstr[i].equals(“”
返回true,则
i
变为4,则跳过else,并无限增加
i

只需使用
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");
            }
        }
    }
  }
}
import java.util.*;
类HelloWorld{
公共静态void main(字符串参数[]){
扫描器ob=新扫描器(System.in);
int t=ob.nextInt();
ob.nextLine();
int a=0,c=0;
对于(int j=0;j0){
字符串[]spstr=str.trim().split(\\s+);
尝试
{

对于(int i=0;i为什么其他问题的答案没有帮助?答案是添加一行
ob.next();
,但是这样做也没有帮助我在catch块中,您没有编写这行
ob.nextLine()根据我在问题中链接的帖子:当扫描器抛出InputMismatchException时,扫描器不会传递导致异常的令牌,因此可以通过其他方法检索或跳过它。那么令牌是如何传递到下一个输入的呢?是的,我试过了,它工作得非常好。谢谢你的帮助您的帮助:)实际上我忘记使用
trim()
在分割输入时,我想如果有空的空间,它会将其作为数据,并从输入中存储一个较小的整数,因此每当发生这种情况时,我都会减少
I
,这样我就可以得到我的输入。但是正如你所说,我在代码中犯了很多错误。现在我得到了我想要的结果。非常感谢你的帮助@卡布
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");
            }
        }
    }
  }
}