0

I'm trying to read this input from the command line

1 2 3
4 5 6
7 8 9

But since it doesn't have an EOF the program throws a runtime error. I'm reading it in the following way inside a loop:

String holder = scanner.nextLine();

Any ideas of what I am missing here?

This is the code I'm using

package com.company;
public class Main {

public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(System.in);
    int numCases = scanner.nextInt();
    scanner.nextLine();
    for(int i = 0; i < numCases; i++) {
        int matrixSize = scanner.nextInt();
        String ar = scanner.nextLine();
        int matrix[][] = new int[matrixSize][matrixSize];
        String rowNumbers[];
        for(int row = 0; row < matrixSize; row++) {
            String holder = scanner.nextLine();
            rowNumbers = holder.split( " ");
            for(int col = 0; col < matrixSize; col++) {
                matrix[row][col] = Integer.parseInt(rowNumbers[col]);
            }
        }

        int trace = calculateMatrixTrace(matrixSize, matrix);
        int repeatedInRows = countRepeatedInRows(matrixSize, matrix);
        int repeatedInCols = countRepeatedInCols(matrixSize, matrix);

        System.out.println("Case #" + (i + 1) + ":" + " " + trace + " " + repeatedInRows + " " + repeatedInCols);
    }
}

static int calculateMatrixTrace(int matrixSize, int matrix[][]) {
    int matrixTrace = 0;
    for(int i = 0; i < matrixSize; i++) {
        matrixTrace += matrix[i][i];

    }

    return matrixTrace;
}

static int countRepeatedInCols(int matrixSize, int matrix[][]) {
    int repeatedInCols = 0;
    List<Integer> numbersInCols = new ArrayList<>();
    for(int col = 0; col < matrixSize; col++) {
        for(int row = 0; row < matrixSize; row++) {
            int currentNumber = matrix[row][col];
            if (row != 0 && numbersInCols.contains(currentNumber))  {
                repeatedInCols += 1;
                break;
            }
            numbersInCols.add(matrix[row][col]);
        }
        numbersInCols.clear();
    }

    return repeatedInCols;
}

static int countRepeatedInRows(int matrixSize, int matrix[][]) {
    int repeatedInRows = 0;
    List<Integer> numbersInRows = new ArrayList<>();
    for(int row = 0; row < matrixSize; row++) {
        for(int col = 0; col < matrixSize; col++) {
            int currentNumber = matrix[row][col];
            if (col != 0 && numbersInRows.contains(currentNumber))  {
                repeatedInRows += 1;
                break;
            }
            numbersInRows.add(matrix[row][col]);
        }
        numbersInRows.clear();
    }
    return repeatedInRows;
}

}

ericgramirez
  • 170
  • 1
  • 12

1 Answers1

0

Try something like this:

// enter number of lines
int end = scanner.nextInt();
scanner.nextLine(); // clear lingering line delimiter.
for (int i = 0 i < end; i++) {
   String line = scanner.nextLine();
    // rest of your code here.
}

Another way is to enter a terminating token like "quit".

for (;;) {
   String line = scanner.nextLine();
   if (line.trim().equalsIgnoreCase("quit") {
      break;
    }
    // rest of your code here.
}

It is always useful to print out input prompts like so:

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter # cases: ");
        int numCases = scanner.nextInt();
        scanner.nextLine();
        for(int i = 0; i < numCases; i++) {
            System.out.print("Enter mat dimension: " );
            int matrixSize = scanner.nextInt();
            String ar = scanner.nextLine(); 
            // rest of code omitted
WJS
  • 30,162
  • 4
  • 20
  • 36
  • I'm not able to read the last line with scanner.nextLine(). That's the thing. – ericgramirez Apr 04 '20 at 18:17
  • I don't understand. Can you give an example. Both of the above read in all lines required. – WJS Apr 04 '20 at 18:19
  • The first example is the same thing I'm doing, but since it doesn't have EOF I'm unable to read the line. I can read the first two lines. – ericgramirez Apr 04 '20 at 18:26
  • Did you see the part about clearing the lingering newline. That is still there after you enter a value so you need to get rid of it before reading in your data. – WJS Apr 04 '20 at 18:27
  • Yes, I'm also doing that. I just wrote the code that's trying to read the 3 lines, my bad. – ericgramirez Apr 04 '20 at 18:31
  • Edit your answer and post your code. And what system are you using and/or IDE? – WJS Apr 04 '20 at 18:37
  • I'm using Arch Linux and IntelliJ IDEA – ericgramirez Apr 04 '20 at 18:40
  • It works fine for me. You realize you are prompting for the matrix size for each case? So you need to enter that each time and then enter your values. – WJS Apr 04 '20 at 19:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210948/discussion-between-wjs-and-ericgramirez). – WJS Apr 04 '20 at 19:06
  • Did you see my last comment. Your program works fine. Just remember that after each line you must hit the enter key. – WJS Apr 04 '20 at 20:12