gpt4 book ai didi

java - 为什么我从下面的代码中收到错误java.util.NoSuchElementException?

转载 作者:行者123 更新时间:2023-12-01 06:01:21 24 4
gpt4 key购买 nike

我从下面的代码中收到此错误java.util.NoSuchElementException,我不知道为什么。我正在尝试编写一个读取整数,单词列表和字符的代码。整数表示列表中有多少个单词。程序的输出是列表中每个包含至少一个字符的单词。

       public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] arr = new String[n];
for(int i = 0; i < n; ++i) {
arr[i] = in.next();
}
char ch = in.next().charAt(0); //line that launches the error
for(int i = 0; i < n; ++i) {
if(arr[i].contains(""+ch)) {
System.out.println(arr[i]);
}
}
}

最佳答案

我已经阅读了这段代码,并且还在我的编译器上运行了这段代码。我没有在此代码中看到任何错误。我认为这是您的编译器问题。我建议您重新安装编译器。Here is your code and output:

关于java - 为什么我从下面的代码中收到错误java.util.NoSuchElementException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com