简体   繁体   中英

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.

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. You should probably check if you have data to consumer using hasNextInt like:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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