0

I'm trying to get a 2D array out of a file and into my program. I'm not sure what the size of the 2D arrays is and it has to be sorted in descending order. If I type in a sample array into my code the for statement executes perfectly. My best frustration is getting a 2D array from another file and making my code run and execute it. Can anyone help me with this..

Code:

public class Project2 {
public static void main(String[] args) throws Exception {
  Scanner sc = new Scanner(new File("prog2.txt"));
  int number = 0;
  while (sc.hasNext()) {
    sc.nextLine();
    number++;
  }
  System.out.println(number);

This is what I have to get the file that I need. This displays the number of rows there are.

Code:

for (int i = 0; i < hours.length; i++) {
  int sum = totalHours(hours, i);
  System.out.println("Employee " + i + ": " + sum);
  }
}

This is the for statement that well execute perfectly whenever I manually type in numbers for the 2D array.

I'm trying to get the numbers from the file to run through this for statement. I'm completely confused on how to do this and I've been stuck on this for hours now. Thank y'all in advance for all the help!

  • how are you storing array in file. post the sample from that file too. – Gaurav Gupta Jul 15 '14 at 04:06
  • How does the input file format look like and how do you convert it to a 2D array? – evanwong Jul 15 '14 at 04:06
  • possible duplicate of [Arrays from a different file in Java](http://stackoverflow.com/questions/24748570/arrays-from-a-different-file-in-java) – paisanco Jul 15 '14 at 04:07
  • @paisanco Possible? This is a *definite* duplicate. – McLovin Jul 15 '14 at 04:14
  • This question must be closed, as the OP has already asked this before. – Mustafa sabir Jul 15 '14 at 04:15
  • @user3822460 If this question is at all different from your previous one, could you please explain how? If not, I will flag as duplicate as well. – takendarkk Jul 15 '14 at 04:17
  • Everyone else isn't understanding what I'm saying. So I broke the code down into different parts. I know how the for statement works whenever the array in with the code. I'm not understanding how to read the array from a different file. I thought this would make it easier to understand what I was trying to get at. – user3822460 Jul 15 '14 at 04:20
  • @user3822460 You won't be reading array from other file, read stuff will be String. Now after reading string from other file, you have to decode that string and make an array. for decoding purpose you can delimit array values with some delimiter and after reading string you can use `ur_string.split(delimeter)` to decode the string into string[]. – Gaurav Gupta Jul 15 '14 at 04:24

1 Answers1

0

Hope this may help you.

java file

public class ReadFile {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("./assets/prog2.txt"));
        int[][] storedArray = new int[3][3];
        int i = 0, j = 0;
        while (sc.hasNext()) {
            String line = sc.nextLine();
            String[] rows = line.split(":");
            for (String row : rows) {
                String[] cells = row.split("~");
                j = 0;
                for (String cell : cells) {
                    // System.out.println(cell);
                    storedArray[i][j] = Integer.parseInt(cell);
                    j++;
                }
                i++;
            }
            for (int[] p : storedArray) {
                System.out.println(Arrays.toString(p));
            }
        }
    }
}

prog2.txt

1~2~3:4~5~6:7~8~9

project directory structire

  project
    |---src--javafile
    |
    |---assets---prog2.txt

Assumption

  1. ~ is delimiter used when we want to separate two elements in the 2-D array in same row.
  2. : is delimiter used when we want to separate two rows within 2-D array.
  3. For this example size of 2-D array is restricted to 3x3. for flexible size, checking at runtime needs to added.

Output

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
  • Assumption #3 does not have to be there, if you know what an [`ArrayList`](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) is. – sampathsris Jul 15 '14 at 04:53
  • It seems like OP is storing back the stuff in 2D array, it is evident from the for loop. May be storing in 2D array is his requirement. If OP want has an option of storing value in list, he certainly can. – Gaurav Gupta Jul 15 '14 at 04:57
  • Maybe you missed this part from original question: _I'm not sure what the size of the 2D arrays is..._. So it _has_ to be a dynamically growing collection. – sampathsris Jul 15 '14 at 05:12
  • @Krumia Correct, now it turns out that OP has to put result in arraylist instead of 2D array. I think he will be able to do so, that's not a big challenge. If he request for that too, i will update the answer. – Gaurav Gupta Jul 15 '14 at 05:19