0

need a little help. In the program I'm working on we are supposed to read in a text file titled prog2.dat whose contents are to represent the hours worked by 10 employees. Here is what the file looks like.

10
8  4  7  3  8  6  3 
2  7  6  3  5  2  1  
1  2  3  8  6  4  4 
3  2  8  8  8  5  1 
4  3  2  1  3  8  6 
8  5  6  7  5  5  4 
1  8  7  4  2  8  6 
1  5  4  6  5  3  3
4  3  2  1  2  3  4
1  8  7  6  5  6  5

When the file is read in, it's to be stored in a 2D array, this is where I'm having trouble. My experience with 2D arrays is very minimal and I've never loaded one from a file before.

That's not the end of the program, but everything else is stuff that I know how to do so shouldn't be a problem, it's just getting to that point.

Here is the code I have so far.

import java.util.Scanner;

public class Program2 {
    public static void main(String[] args) {
        int[][] hoursArray = new int[i][j];
        int employeeNum;
        int i = 0;
        int j = 0;
        java.io.File file = new java.io.File("../instr/prog2.dat");
        Scanner fin = new Scanner(file);
             employeeNum = fin.nextInt();
             while (fin.hasNextLine && i < hoursArray.length) {
                hoursArray[i] = fin.nextInt();
                i++; 
            }

I know this is incomplete, but that's what I have so far. Any help would be greatly appreciated.

  • You have `hoursArray[i]` in there but it is a 2d array. You need something like `hoursArray[i][j]`. You should also describe what isn't working or post any errors/exceptions you are getting. – takendarkk Jul 15 '14 at 03:55
  • Ok, that's one thing I was wondering about. I'm confused as to how to loop through this. I found an older program I'd written about a year ago loading a regular one dimensional array from a file and this is what I kind of modeled my while loop after, but am unsure how to make it work for a 2D array? –  Jul 15 '14 at 03:57
  • Write it out step by step on paper to see what you would actually have to do. From there you can translate those steps into code. An example would be 2 for loops nested inside of each other like `for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { } }`. Also, you have your 2d array declaration using the variables `i` and `j` before you create `i` and `j`. That will be a problem. – takendarkk Jul 15 '14 at 03:59
  • Ok, thanks. I have pretty much everything written out on paper, it's just like I said, I've never really worked with this type of thing before. And thanks, I'll fix that, my professor likes us to declare everything at the top, but if it's gonna mess things up he'll just have to get over it haha. Thanks again. –  Jul 15 '14 at 04:05
  • I would not use `fin.nextInt()` to read the data. The problem is that `Scanner` doesn't care about newlines when you're using `nextInt`, so it looks like just one big sequence of numbers. But you care about the newlines since you are treating the input as a 2D array. Consider using `nextLine()` to read each line, then for each line, you can create a new `Scanner` to scan the line as a `String`. (Yes, you can create a `Scanner` from a `String`. See the [javadoc](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#Scanner-java.lang.String-)). – ajb Jul 15 '14 at 04:06
  • @BethTanner In terms of declaring everything at the top, you can still do that. It's just that you need to declare `i` and `j` _before_ you declare the 2d array. Otherwise, when you put `int[][] hoursArray = new int[i][j];` the compiler doesn't yet know what `i` and `j` are because you declare those later. Just switch the order of them. With that in mind, if you start `i` and `j` at 0 you will be making a 2d array with 0 length which is definitely not what you want. – takendarkk Jul 15 '14 at 04:11

1 Answers1

0

By using java.util.List you don't need to worry about knowing size (length) before hand. You can always convert back to an array with List.toArray(Integer[]).

But if you are insisting on using Arrays (maybe this is your assignment requirement), then you need to know about the dimension of the 2d array you want before you can start using it. You know how many employee there is, as the first line tells you. You know it is going to be 7 numbers for each employee as it is for 7 days in the week. (I am assuming this is in your assignment requirement.) So you going to have

Integer[][] hoursArray = new Integer[employeeNum][7];

then you are going to read each number in turn. I suggest you keep two counters, eId for employee id, and d for day. Both initialised to 0. Then d gets incremented by 1 after each time a number is read, until d equals to 7, then d reset back to 0, and eId gets incremented by 1. So now the number can be place into the array when read like so

hoursArray[eId][d] = fin.nextInt();

Of course you still need to handle any input errors, but I leave it to you now.

To understand more about 2d arrays in java, I suggest have a look at this Syntax for creating a two-dimensional array

Community
  • 1
  • 1
nzCharlie
  • 36
  • 2
  • Thank you, this makes a lot of sense. Let me make sure I'm writing this out correctly though if you don't mind? for(int eid = 0; eid < employeeNum; eid++) for(int d = 0; d < 7; d++) hoursArray[eid][d] = fin.nextInt(); Is that close to being correct? –  Jul 15 '14 at 04:38
  • Yes, this is correct, given that each line for the employee hours worked has 7 numbers. – nzCharlie Jul 15 '14 at 04:42
  • As someone above suggested, because the Scanner class does not care about new lines, so it may still appears to work even if the assumption is broken, but the output will be undefined (i.e. bad!) Better way is to read each line, tokenize the line into number array (see http://stackoverflow.com/questions/2173855/using-string-tokenizer-to-set-create-arrays-out-of-a-text-file). It is more robust and easier to change if the above requirement changes. – nzCharlie Jul 15 '14 at 04:52