gpt4 book ai didi

java.util.NoSuchElementException : No line found

转载 作者:行者123 更新时间:2023-11-30 05:28:49 24 4
gpt4 key购买 nike

我已经在 StackOverflow 上搜索了与此错误相关的每个问题,但相信我,我的情况有所不同。实际上,我正在准备竞争级别的编程技能,并且我在笔记本电脑上成功解决了这个问题,并且输出是 100%真实的,没有任何类型的错误,对于这个黑客地球问题(下面的链接)-

https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/#c198374

但是当我尝试在黑客地球上提交我的代码时,它会抛出一个错误,现在我真的很困惑为什么当它在我的笔记本电脑上成功运行时会出现错误。下面的错误截图 - enter image description here这是我的代码 -

import java.util.*;

class nalin{
public static void main(String args[]){
Scanner bob = new Scanner(System.in);
int bobs = bob.nextInt();
String result[] = new String[bobs];
for(int g = 0; g<bobs; g++){
Scanner s = new Scanner(System.in);
String x = s.nextLine();
String arr[] = x.split("\\s+");
int coun = 0;
char v1[] = arr[0].toCharArray();
char v2[] = arr[1].toCharArray();

for(int i = 0; i<v1.length; i++){
for(int j = 0; j<v2.length; j++){
if(v1[i] == v2[j]){
coun = coun+1;
break;
}
}
}
if(coun == v1.length){
result[g] = "YES";
}else{
result[g] = "NO";
}
}
for(int l = 0; l<result.length; l++){
System.out.println(result[l]);
}
}
}

最佳答案

注意:仅使用散列概念。尝试在 O(字符串长度) 内完成。提及问题本身。而且你的逻辑也是错误的(检查你用于字符串比较的嵌套循环。)此外,你还减速了扫描仪两次。删除 for 循环内的 1

您正在使用扫描仪类的 nextLine 方法。以下是 nextline() 说明

nextLine
public String nextLine()


Advances this scanner past the current line and returns the inputthat was skipped.This method returns the rest of the current line, excluding any lineseparator at the end. The position is set to the beginning of the nextline.
Since this method continues to search through the input lookingfor a line separator, it may buffer all of the input searching forthe line to skip if no line separators are present.

Returns:the line that was skipped

Throws:NoSuchElementException
1 - if no line was foundIllegalStateException
2 - if this scanner is closed.

下面是工作代码 -但我并没有纠正你的逻辑。

package Array;

import java.util.*;

class nalin {
public static void main(String args[]) {
Scanner bob = new Scanner(System.in);
int bobs = bob.nextInt();
String result[] = new String[bobs];
for (int g = 0; g < bobs; g++) {
String x = bob.next();
String y = bob.next();
//String arr[] = x.split("\\s+");
int coun = 0;
char v1[] = x.toCharArray();
char v2[] = y.toCharArray();

for (int i = 0; i < v1.length; i++) {
for (int j = 0; j < v2.length; j++) {
if (v1[i] == v2[j]) {
coun = coun + 1;
break;
}
}
}
if (coun == v1.length) {
result[g] = "YES";
} else {
result[g] = "NO";
}
}
for (int l = 0; l < result.length; l++) {
System.out.println(result[l]);
}
}
}

关于java.util.NoSuchElementException : No line found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57998737/

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