I'm trying to read a 2D array from a certain file. I've got the code so it tells me how many arrays there are in the file, but my problem is I'm not sure how to finish the for statement. This is what I have so far....
import java.util.*;
import java.lang.*;
import java.io.*;
public class Project2 {
/*Main Method*/
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("prog2.txt"));
int number = 0;
while (sc.hasNext()) {
sc.next();
number++;
}
System.out.println(number);
/*int [][] hours = {
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9},
};*/
for (int i = 0; i < hours.length; i++) {
int sum = totalHours(hours, i);
System.out.println("Employee " + i + ": " + sum);
}
}
public static int totalHours(int[][] time, int rowIndex) {
int total = 0;
int i = 0;
for (int column = 0; column < time[i].length; column++) {
total += time[rowIndex][column];
}
return total;
}
}
The int[][] hours was example given in the book. The for statement is setup for those values. I'm trying to get the values from a certain file(the values are unknown) to work with this for statement. Can anyone help me with this?