3

Quick question, how would I go about reading a txt file into a 2d array? I need to but each line into an array in order to later on use it for measuring summation of length and width. This will be used for the game of life.

CODE

public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int matrix[][] = new int[size][size];
            while (s.hasNext()) {
                for (int i = 0; i < matrix.length; i++) {
                       for (int col = 0; col < matrix.length; col++) {
                      //      matrix[i][col] = ;
                        }
                }
            }
            s.close();

        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

TEXT DOCUMENT

20 20
    0    2
    0    6
    0    7
    0   10
    0   11
    0   16
    1    0
    1    4
    1    7
    2    6
    2   12
    2   15
    2   19
    3    0
    3    4
    3   14
    3   16
    3   17
    4    0
    4    8
    4   12
    4   14
    4   15
    5    0
    5    2
    5    4
    5    7
    5   13
    5   17
    6   11
    6   14
    6   15
    6   18
    7    4
    7   10
    7   13
    7   14
    8    6
    8   11
    8   12
    8   14
    8   17
    8   19
    9    6
    9    8
    9   13
    9   14
   10    3
   10   10
   10   19
   11    6
   11    7
   11    9
   11   10
   11   17
   12    0
   12    2
   12   12
   13    0
   13    1
   13    7
   13    8
   13   14
   13   15
   14    7
   14    9
   14   12
   14   13
   14   16
   14   18
   15    0
   15    2
   15    6
   15    8
   15   10
   15   11
   15   16
   15   17
   16    2
   16    3
   16   18
   17    2
   17    7
   17   11
   17   13
   17   19
   18    0
   18    5
   18    6
   18   11
   18   12
   18   13
   18   14
   19    3
   19    4
   19    6
   19    8
   19   11
   19   16
   19   17

4 Answers4

2
public int[][] P4_Icel_Murad_Life(String fileName) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));

        // n = Rownum ; m = Colnum
        int array[][] = new int[n][m];
        String line = reader.readLine();

        int i = 0, j = 0;
        while (line != null) {
            String strArray[] = line.split(" ");

            if (!line.trim().isEmpty()) {
                for (String s : strArray) {
                    if (!s.trim().isEmpty()) {
                        array[i][j++] = Integer.parseInt(s);
                    }
                }
                line = reader.readLine();
                i++;
                j = 0;
            }
        }
        reader.close();
        return array;
    } catch (IOException ex) {
        System.out.println("Problems..");
    }
    return null;
}
Davit Mumladze
  • 918
  • 2
  • 9
  • 25
2

So it seems that the textfile contain all pairs at which where is life in the game of life.

What you do is increase the "i" value for each read, but that is not garanteed.

Instead the following protocol is the one of the text file it seems.

  1. Line contain the X and Y size of the board.

The rest of the lines contains spots where there is life.

So your code should look more like this(not included exception handling apart from what you already provided)

 public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int rows = s.nextInt();
            int columns = s.nextInt();
            int matrix[][] = new int[rows][columns];
            while (s.hasNextInt()) {
                int row = s.nextInt();
                int col = s.nextInt(); // If this fails the text documnet is incorrect.
                matrix[row][col] = 1;
            }
            s.close();
            // At this point all dead cells are 0 and all live are 1
        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }
Wisienkas
  • 1,602
  • 2
  • 17
  • 22
0

You are interested in int values, so use this.

Also, the hasNextLine condition has been moved into the rows loop, and a hasNextInt condition added to the columns loop.

public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int matrix[][] = new int[size][size];

                for (int i = 0; i < matrix.length && s.hasNextLine(); i++) {
                       for (int col = 0; col < matrix.length && s.hasNextInt(); col++) {
                           matrix[i][col] = s.nextInt() ;

                        }
                       s.nextLine(); // col values populated for this row, time to go to the next line
                }

            s.close();

        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }
Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

You can use opencsv to read and write such kind of a file

 CSVReader reader = new CSVReader(
                new FileReader("C:/Users/muro0/Desktop/life100.txt"), ' ');
 // Read all rows at once
 List<String[]> allRows = readAll(reader);

It reads file to strings, but you can convert it to integers of course.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67