简体   繁体   English

java.util.Scanner上的java.util.NoSuchElementException

[英]java.util.NoSuchElementException at java.util.Scanner

I am solving problems at HackerEarth and I am not able to figure out why my program in running correctly on my command line and giving right result but when running on their code editor it is giving java.util.NoSuchElementException exception. 我正在HackerEarth解决问题,无法弄清为什么我的程序在命令行上正确运行并给出正确的结果,但是在其代码编辑器上运行时却给出了java.util.NoSuchElementException异常。

I searched but couldn't resolve it. 我搜索了但无法解决。

import java.util.Scanner;

public class TestClass {
    public static int[][] arr = null;
    public static int[][] dp = null;

    public static void main(String[] args) {
        int N, M, T;
        int min;
        int count = 0;
        Scanner scan = new Scanner(System.in);

        N = scan.nextInt();
        M = scan.nextInt();

        arr = new int[N][M];
        dp = new int[N + 1][M + 1];

        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < M; ++j) {
                arr[i][j] = scan.nextInt();   //line 26
            }
        }

        for (int i = 0; i < M + 1; ++i)
            dp[0][i] = 0;

        for (int i = 0; i < N + 1; ++i)
            dp[i][0] = 0;

        for (int i = 1; i < N + 1; ++i) {
            for (int j = 1; j < M + 1; ++j) {
                if (arr[i - 1][j - 1] == 0) {
                    min = Math.min(dp[i - 1][j], Math.min(dp[i]
                        [j - 1], dp[i - 1][j - 1]));
                    dp[i][j] = min + 1;
                } else
                    dp[i][j] = 0;
            }
        }

        count = 0;
        for (int i = 1; i < N + 1; ++i) {
            for (int j = 1; j < M + 1; ++j) {
                if (dp[i][j] != 0)
                    count += dp[i][j];
            }
        }
        System.out.println("" + count);
    }//main
}//class

Exception: 例外:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at TestClass.main(editor_trsource_1429355115_85417.java:26)

This means you don't have data from standard input and you are trying to get nextInt from the same. 这意味着您没有来自标准输入的数据,并且试图从同一输入中获取nextInt You should probably check if you have data to consumer using hasNextInt like: 您可能应该使用hasNextInt检查是否有数据提供给消费者:

if (scanner.hasNextInt()) {
   //read nextInt();
}

如果您要在编程竞赛中通过在线编译器提交解决方案,请尝试刷新浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM