I have a 2D array that displays a table of numbers
I'm trying to populate a my 2D array using these coordinates.
My goal is to place place the number 1 at each of the coordinates. I'm trying to use a hasNext() method to read the txt file and then populate the array but im not having any luck. How do I read in each row and column of integers into the correct location.
Here is what I have so far:
import java.util.*;
import java.io.*;
public class CharacterClasses {
static final int rows = 6;
static final int columns = 5;
public static void main(String[] args) throws Exception {
int[][] surveyMatrix = new int[rows][columns];
initMatrix(surveyMatrix);
printArray(surveyMatrix);
}
// use this to populate survey matrix
public static void initMatrix(int[][] M) throws Exception {
File myFile = new File("survey2.txt");
Scanner sc = new Scanner(myFile);
while (sc.hasNextInt()) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
M[r][c] = sc.nextInt();
}
}
}
}
public static void printArray(int [][] array){
System.out.printf("%15s %23s\n", "Character Class", "Damage Range");
System.out.printf("%41s\n", "1 2 3 4 5");
String[] names = {" Huntress:", " Priest:", " Summoner:",
" Necromancer:", " Demon Knight:", " Berserker:"};
for (int row = 0; row < rows; row++) {
System.out.printf("%5s ", names[row]);
for (int col = 0; col < columns; col++) {
System.out.printf("%5d", array[row][col]);
}
System.out.println();
}
}
}