Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.util.NoSuchElementException:未找到任何行_Java - Fatal编程技术网

java.util.NoSuchElementException:未找到任何行

java.util.NoSuchElementException:未找到任何行,java,Java,我已经在StackOverflow上搜索了与此错误相关的所有问题,但相信我,我的情况与此不同。事实上,我正在准备有竞争力的编程技能,我在笔记本电脑上成功地解决了这个问题,输出是100%真实的,没有任何类型的错误,这个问题的黑客地球(链接如下)- 但当我试图在hacker earth上提交代码时,它会抛出一个错误,现在我真的很困惑,为什么在我的笔记本电脑上成功运行时会出现错误。 下面是错误截图- 这是我的密码- import java.util.*; class nalin{ publ

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

但当我试图在hacker earth上提交代码时,它会抛出一个错误,现在我真的很困惑,为什么在我的笔记本电脑上成功运行时会出现错误。 下面是错误截图- 这是我的密码-

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]);
    }
    }
}
import java.util.*;
纳林类{
公共静态void main(字符串参数[]){
扫描器bob=新扫描器(System.in);
int bobs=bob.nextInt();
字符串结果[]=新字符串[bobs];

对于(int g=0;g将扫描仪声明置于循环之外:

    Scanner s = new Scanner(System.in);
    int bobs = s.nextInt();
    for(int g = 0; g<bobs; g++)
    {
    }
Scanner s=新的扫描仪(System.in);
int bobs=s.nextInt();

对于(int g=0;g注意:仅使用散列概念。尝试在O(字符串长度)中进行此操作。提及问题本身。并且您的逻辑也错误(检查用于字符串比较的嵌套循环)。此外,您已将扫描仪减速2次。删除循环内部的1

您正在使用scanner类的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.*;
纳林类{
公共静态void main(字符串参数[]){
扫描器bob=新扫描器(System.in);
int bobs=bob.nextInt();
字符串结果[]=新字符串[bobs];
对于(int g=0;g
注意:仅使用散列概念。请尝试在O(字符串长度)中执行此操作。提到问题本身。您的逻辑也是错误的。请检查用于字符串比较的嵌套循环。此外,您已将扫描程序减速2次。为循环删除1次。您向扫描程序声明了同一个ressource,您不应该这样做,因为它们相互冲突。扫描程序缓冲输入,它们读取的内容比您告诉它们的要多阅读,以提高性能。此外,您正在将
nextInt
nextLine
混合使用,这会给您带来一些麻烦,因为
nextXXX
没有按照您的想法进行操作(请参阅相应的SO线程)。使用扫描仪,您需要检查是否有下一行hasNextLine()@SSP如果我在循环中删除了when scanner,那么我会抛出一个错误-indexootfound,这就是为什么我两次包含它…是的,我知道我的逻辑有点错误,但当我编译这个时,大约70%的案例都有正确答案code@sarel那么为什么这个代码在我的笔记本电脑上运行呢?是的,我把它放进去了,但是没有任何变化。错误仍然是一样的,但是等等让我再试一次我还是会出错…我给了你一个链接,你自己试一下,兄弟..希望你能找到其他方法,你能给我一些例子或0(字符串长度)的链接吗如果我删除了循环内的扫描仪,那么我会抛出一个错误-indexoutofbound,这就是为什么我两次包含它…是的,我知道我的逻辑有点错误,但当我编译此代码时,大约70%的情况都有正确的答案我的问题是编译时不使用逻辑bro…我稍后会更正我的逻辑,如果它有帮助,我会接受答案。我感谢你的工作,并接受它没关系,我不需要逻辑,但你从代码中删除了一些行并粘贴在这里…1)我如何获取字符串你删除了该行而不在代码中包含任何位置来获取字符串…兄弟,我也可以通过删除一些行来更正我的答案:-)我需要更正兄弟,而不是删除