java.util.NoSuchElementException : No line found

标签 java

我已经在 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/

相关文章:

Java aes key 到 32 字节字符串表示

java - 如何将一个Master上的方法调用交给n个Workers并返回?

java - IntrumentModels 的任务 : Model class is frozen

java - 在 jar 中找不到属性文件

java - 移动 Java Swing 图形

java - 如何在android ImageView 中显示tomcat中本地服务器的图像

java - 使用正则表达式读取聊天消息

java - 如何让eclipse在打开时将依赖的maven项目部署到tomcat?

java - org.jacoco :jacoco-maven-plugin 的生命周期配置未涵盖插件执行

java - 我可以将上传的文件存储在 FileInputStream 对象中吗?