首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java.util.NoSuchElementException :找不到行

java.util.NoSuchElementException :找不到行
EN

Stack Overflow用户
提问于 2019-09-19 02:20:55
回答 2查看 202关注 0票数 1

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

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

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

这是我的代码-

代码语言:javascript
运行
复制
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]);
    }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-09-19 02:29:39

注意:仅使用散列概念。尝试使用O(字符串长度)。提到问题本身。而且您的逻辑也是错误的(检查用于字符串比较的嵌套循环。)另外,您已将扫描仪拆下2次。移除for循环中的%1

您正在使用scanner类的nextLine方法。下面是nextline()描述

代码语言:javascript
运行
复制
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.

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

代码语言:javascript
运行
复制
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]);
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-09-19 02:26:35

将scanner声明放在循环之外:

代码语言:javascript
运行
复制
    Scanner s = new Scanner(System.in);
    int bobs = s.nextInt();
    for(int g = 0; g<bobs; g++)
    {
    }

在整个执行过程中使用单个扫描器

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57998737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档