Cyclotron3x3
Cyclotron3x3

Reputation: 2229

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)

Upvotes: 0

Views: 5719

Answers (2)

cammando
cammando

Reputation: 616

If you are submitting solution on online comiler in a programming competition try to refresh the browser.

Upvotes: 0

SMA
SMA

Reputation: 37023

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();
}

Upvotes: 3

Related Questions