0

I'm reading in numbers to fill an adjacency matrix. The numbers are read in from a file and the format can be seen below.

0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0

But I'm getting an InputMismatchException when I try to run my program, and I can't figure out why. My code is below. I'd greatly appreciate some help.

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {
    Scanner scanner = new Scanner("sample1.txt");
    scanner.useDelimiter("[\\s,]+");

    int input = scanner.nextInt();
    int[][] adjMatrix = new int[8][8];

    for(int i=0; i < input; i++) {
        for (int j=0; j < input; j++) {
             adjMatrix[i][j] = scanner.nextInt();
        }
    }
    scanner.close();

    new DFS(adjMatrix);

}

} 
  • 6
    `Scanner scanner = new Scanner("sample1.txt");` reads from `"sample1.txt"` - not a `File` with that name. The `String`. You wanted `Scanner scanner = new Scanner(new File("sample1.txt"));` – Elliott Frisch Mar 19 '18 at 16:46
  • You're right! Can't believe I left that out. Thanks! –  Mar 19 '18 at 16:47
  • 1
    Also notice that in your example data, you do not store the size of the matrix as the first value, while the code expects it. – 9000 Mar 19 '18 at 16:48
  • Thanks, I've edited my code (and example) to reflect that. Think it should be `int[][] adjMatrix = new int[8][8];` –  Mar 19 '18 at 17:03

0 Answers0