-1

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?

  • Can you please specify how the data is stored in the file ? (one value per line OR one row per line etc. etc.) – Utsav T Jul 15 '14 at 01:28
  • We definitely need to know more about how the file is set up. Right now, it looks like you're assuming that it's just a bunch of numbers, and you are figuring out how many numbers there are in the file--but if it's a 2D array, you can't tell how many rows and how many columns there are, just by counting how many numbers there are in the file. There has to be some other structure to the file. – ajb Jul 15 '14 at 01:31
  • 1
    Your code looks fine so far. Continue and try reading the file and construct the 2d array. If you run into any *specific* issue - post it here. – Nir Alfasi Jul 15 '14 at 01:35
  • I wasn't told what the file would be. Just that it would be a 2D array. I know that it will be Int's but that's all. I see how the commented out array looks but that was in the book. I don't know what's going to be on the file. – user3822460 Jul 15 '14 at 01:46
  • this is what I'm told......... Suppose the weekly hours for all employees are stored in a 2D array. Each row records an employee's seven day work hours with seven columns. Write a program that displays employees and their total hours in decreasing order of total hours from a file. Also read the number of employees first. – user3822460 Jul 15 '14 at 01:50
  • OK, it looks like the *first* number that you scan from the file is going to be the number of employees. The phrase "from a file" seems pretty disconnected, though--it doesn't seem to give enough info. Assuming this is homework, I think you need to go back and ask your teacher for more info about what the file looks like. That's what I would do, since I can't figure out what the assignment means. – ajb Jul 15 '14 at 03:46
  • Are you in the same class as the person who asked [this question](http://stackoverflow.com/questions/24749388/loading-a-2d-array-from-a-file)? – ajb Jul 15 '14 at 04:01
  • It seems to be the same concept. – user3822460 Jul 15 '14 at 04:03

2 Answers2

0
column < time[i].length

i should be rowIndex

XoXo
  • 1,560
  • 1
  • 16
  • 35
0

You can iterate through your rows and columns this way:

for (int row = 0; row < hours.length; row++) {

int total = 0;

  for (int column = 0; column < hours[raw].length; column++) {

      total += hours[row][column];
  }

system.out.println("Row total: " + total);  

}

Muhammad
  • 31
  • 3