1

在IntelliJ上运行代码时没有收到错误消息。但是,当我尝试提交我正在处理的作业的代码时,我得到了两个测试用例的 NFE。我删除了我的所有代码,只让下面的代码在测试用例中运行。这里的某个地方一定是一个NumberFormatException.

public class Search {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        int[] list = new int[n];
        String [] tokens = sc.nextLine().trim().split(" ");

        for (int i=0; i<tokens.length;i++){
            list[i]=Integer.parseInt(tokens[i]);
        }
    }
}

我读到了双空格并检查了这一点:System.out.println(Arrays.asList(tokens).contains("")); 输出为假,所以这不是一个选项。如您所见,我已经在使用trim(). 我会很感激你的帮助。路易斯

Eddit:好吧,这里有些可疑。我添加了

System.out.println(Arrays.asList(tokens).contains(""));
    System.out.println(Arrays.toString(tokens));

到我的代码并将其交给测试用例。虽然 IntelliJ 会传递 false 后跟整数数组,但测试用例输出: true [] 。因此,你们都是对的,我只是错误地假设我的测试用例中的输入与我在作业中给出的示例输入相似。

编辑2:

好吧!我想到了。测试用例的输入与我的测试输入中的格式完全不同,看起来有点像这样:

10
8 8 9 12 110 111 117 186 298 321
2
8 13

我假设我包含的 sc.nextLine() 跳过了我需要制作列表的整数。所以实际的问题不是多余的空格或任何东西,只是我通过使用 sc.nextLine() 超越了我想要的输入。给出了我需要的提示的答案,即使我不认为这是有意的,来自 Andronicus。无论如何都要感谢其他人。

4

4 回答 4

2

如果您知道将有一个整数作为输入并且您不担心解析,为什么不使用它呢?

int input = sc.nextInt();

在您的解决方案中,您必须执行以下操作:

Arrays.stream(sc.nextLine().trim().split(" ")).filter(s -> !s.matches("\\s")).toArray(String[]::new);
\\ or simplier
sc.nextLine().trim().split("\\s+")
于 2019-03-11T16:08:48.683 回答
2

有很多可能的原因:

  1. 有一个非数字tokens- 例如。9 1! 3 x 3...
  2. 标记被多个空格分开——例如9 3

您应该能够通过数字格式异常的文本来判断。例如,在多个空格的情况下,您会得到:

线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“”

对于非数字(例如“a”),你会得到:

线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“a”

当然,有许多可能的解决方案取决于您在遇到无效输入时想要做什么(您是否忽略它?抛出一个特殊异常?尝试去除非数字?)

当您知道您的输入由空格分隔,但不知道有多少空格时,您可以使用正则表达式来定位split命令中的多个空格:

str.split("\\s+"); // splits on one or more whitespace including tabs, newlines, etc.

然后,要处理令牌列表中的非数字,您可以在 for 循环中添加检查:

for(int i = 0; i < tokens.length; i++) {
  if(tokens[i].matches("\\d+")) {
    list[i] = Integer.parseInt(tokens[i]);
  } else {
    // Handle error case for non-digit input
  }
}
于 2019-03-11T16:18:12.117 回答
1

这可能是由于数字之间有多余的空格。

例如,

9    8 7 9    1
  ^^       ^^
*Note: You have more than one spaces here.

这就是你的数组在拆分后的样子,

tokens = {"9", "", "", "", "8", "7", "9", "", "", "", "1"}

NumberFormatException由于多余的空格,上面会抛出。

你可以再试trimming一下内容,

int i = 0;
for (String token : tokens){
    token = token.trim();
    if (!"".equals(token)) {
        list[i++] = Integer.parseInt(token);
    }
}
于 2019-03-11T16:09:47.767 回答
0

请将您的代码修改为:

public class Example {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the size of the array : ");
        int n = sc.nextInt();
        sc.nextLine();
        int[] list = new int[n];
        System.out.println("Enter a string : ");
        /** This regex will work for string having more than one space. */ 
        String trimmedToken = sc.nextLine().replaceAll("\\s+", " ");
        String[] tokens = trimmedToken.split(" ");
        for (int i = 0; i < tokens.length; i++) {
            list[i] = Integer.parseInt(tokens[i]);
            System.out.println(list[i]);
        }
        sc.close();
    }

}

控制台输入:

Enter the size of the array : 
5
Enter a string : 
1       2     3      4    5

输出 :

1
2
3
4
5
于 2019-03-11T16:29:30.353 回答