0

I have been running this algorithm in order to solve this CCC (Canadian Computing Contest) question. It runs fine and gives the correct output on IntelliJ but shows an NoSuchElementException in DMOJ and the CCC online grader.

Here is my code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        solution(sc.nextInt());

    }

    public static void solution(int lines) {
        Scanner sc = new Scanner(System.in);
        int[][] sunflowers = new int[lines][lines];
        int[][] temp = new int[lines][lines];

        // Creating sunflowers array
        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < lines; j++) {
                sunflowers[i][j] = sc.nextInt();

            }

        }

        boolean readyToSubmit = false;
        int a = 0;
        int b = 0;

        while (readyToSubmit == false) {
            b = 0;
            a = 0;

            readyToSubmit = true;
            for (int g = 0; g < sunflowers.length - 1; g++) {
                for (int h = 1; h < sunflowers[g].length; h++) {
                    if (sunflowers[g][h - 1] > sunflowers[g][h]) {
                        // Turn true if previous value smaller than current
                        readyToSubmit = false;
                    }
                }
            }

            // If each column is in descending order
            for (int d = 0; d < sunflowers.length; d++) {
                for (int e = 1; e < sunflowers.length; e++) {
                    if (sunflowers[e - 1][d] > sunflowers[e][d]) {
                        readyToSubmit = false;
                    }
                }

            }

            if (readyToSubmit == true) {
                break;
            }


            // Rotating the Array w/ temp
            for (int i = sunflowers.length - 1; i >= 0; i--) { // we want position to go right -> left
                b = 0;
                for (int j = 0; j < sunflowers[0].length; j++) { // We want columns to go up -> down

                    temp[a][b] = sunflowers[j][i];



                    b += 1;

                }
                a += 1;
            }

            for (int x = 0; x < lines; x++) {
                for (int y = 0; y < lines; y++) {
                    sunflowers[x][y] = temp[x][y];

                }
            }

        }

        for (int s = 0; s < sunflowers.length; s++) {
            for (int k = 0; k < sunflowers[s].length; k++) {
                System.out.print(sunflowers[s][k] + " ");
            }
            System.out.println();
        }

    }
}

Input: 3 3 7 9 2 5 6 1 3 4

Output (in IntelliJ):

1 2 3
3 5 7
4 6 9

Output (on DMOJ): IR (java.util.NoSuchElementException)

Output (on CCC Grader):

Exception in thread "main" java.util.NoSuchElementException
<251 more characters> // unfortunately, I am not able to see what the 251 characters are.

I am currently not sure on what causes this NoSuchElementException (since it doesn't tell me the line number on DMOJ nor the CCC grader). Any help would be greatly appreciated.

Henry Wang
  • 161
  • 2
  • 2
  • 14
  • 1
    Remove this line ```Scanner sc = new Scanner(System.in);``` on the ```solution``` method. Then close ```sc``` after ```solution(sc.nextInt());``` line on the main method. Refer to this link -> https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input – Mark Melgo Feb 20 '19 at 05:25
  • @Mark If I remove Scanner declaration in solution method, all code that need scanner in that method won't work! Also, what do you mean by closing sc after solution(sc.nextInt()) ? – Henry Wang Feb 20 '19 at 05:33
  • pass your scanner in the ```solution``` method. Change your ```solution``` method to accept a scanner, so the method signature will be ```public static void solution(int lines, Scanner sc) ```, then call it in your main method by ```solution(sc.nextInt(), sc);```. Then after ```solution(sc.nextInt(), sc);``` close your scanner by using ```sc.close()``` – Mark Melgo Feb 20 '19 at 05:37
  • It works now! Thank you! – Henry Wang Feb 20 '19 at 05:41
  • I added it as an answer so that this problem will be marked as solved. Kindly flag it as the accepted answer :) – Mark Melgo Feb 20 '19 at 05:48
  • He ain't never coming back >.> – Kebab Programmer Feb 20 '19 at 10:37
  • 1
    @KebabProgrammer He came back :p – Mark Melgo Feb 21 '19 at 00:34

1 Answers1

1

Note: This is found in the comment section, I just added it as answer to verify that this problem has been solved.

Remove this line Scanner sc = new Scanner(System.in); on the solution method. Then close sc after solution(sc.nextInt()); line on the main method. Refer to this [link]1 pass your scanner in the solution method. Change your solution method to accept a scanner, so the method signature will be public static void solution(int lines, Scanner sc) , then call it in your main method by solution(sc.nextInt(), sc);. Then after solution(sc.nextInt(), sc); close your scanner by using sc.close()

Mark Melgo
  • 1,477
  • 1
  • 13
  • 30