I'm supposed to take the first two integers from a txt file and create an array using them. Here is the txt file that was given to me.
5 is how many rows are in the array. 6 is how many columns are in the array. I started off with just trying to read the first integer but when I try to run it I get an error.
5
6
68 72 75 81 72 75
92 87 71 83 60 75
90 52 13 63 13 61
84 67 100 99 98 92
51 32 94 76 55 11
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class main {
public static void main(String[] args) throws FileNotFoundException {
File fileToRead = new File("array.txt");
Scanner input = new Scanner(fileToRead);
int grades = input.nextInt();
int[][] array = new int[grades][grades];
while (input.hasNext()) {
for (int row = 0; row < grades; row++) {
for (int column = 0; column < grades; column++) {
array[row][column] = input.nextInt();
}
}
}
input.close();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.println(array[i][j]);
}
System.out.println();
}
}
}
I'm not sure if I'm taking the right approach but this is what I have so far.