[英]Why am I getting the error java.util.NoSuchElementException from my code below?
I'm getting this error java.util.NoSuchElementException from my code below and I can't figure out why.我从下面的代码中收到此错误 java.util.NoSuchElementException ,我不知道为什么。 I'm attempting to write a code that reads an integer, a list of words, and a character.
我正在尝试编写一个读取 integer、单词列表和字符的代码。 The integer signifies how many words are in the list.
integer 表示列表中有多少个单词。 The output of the program is every word in the list that contains the character at least once.
该程序的 output 是列表中包含该字符至少一次的每个单词。
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]);
}
}
}
I have read this code and also run this code on my compiler.我已阅读此代码并在我的编译器上运行此代码。 I don't see any error in this code.
我在这段代码中没有看到任何错误。 I think it is your compiler problem.
我认为这是您的编译器问题。 I suggest you reinstall your compiler.
我建议你重新安装你的编译器。 Here is your code and output:
这是您的代码和 output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.