0

I have been trying to convert a file to a String [] [] for several days now, but I still have an error. I don't know how to proceed.

    String[][] lecture_tab;

    public void readLines() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("lvl1.txt"));

        int i = 0;
        while (i < 15) {
            for (String line; (line = br.readLine()) != null; ) {
                System.out.println(line);
                this.lecture_tab[i] = line;
                i += 1;
            }
        }
        System.out.println(lecture_tab);
    }

The text file looks like this :

###################
###################
###################
###################
###################
###################
###################
###################
###################
###################
###################
###################
###################
###################
###################

/!\ UPDATE /!\ : After listening to your advice i finally got to do what i wanted to do. Well almost, when I try to display my matrix, there is only the first line displayed, the 14 other lines are empty ...

public void readLines() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("lvl1.txt"));

        for (int i = 0; i < lecture_tab.length; i++) {
            for (String line; (line = br.readLine()) != null;) {
                for(int j = 0; j < 19; j++) {
                    this.lecture_tab[i][j] = line.charAt(j);
                }
            }
        }

        for (int i = 0; i < this.lecture_tab.length; i++) {
            for (int j = 0; j < this.lecture_tab[i].length; j++) {
                System.out.print(this.lecture_tab[i][j]);
            }
            System.out.println();
        }
    }

The output :

###################














RandAFR
  • 1
  • 1
  • What should the 2d array look like after you put the content of the file into it? In other words, why is it a `String[][]` and not a `String[]` where every element of the array is a single line? What's the second dimension for? – Federico klez Culloca Nov 12 '21 at 14:30
  • Thanks for your feedback @Federico klez Culloch, I want to have a ```String[][]``` because I'm developing a game, that's why I want to use a matrix. Each box must be checked for example: lecture_tab [x] [y]. – RandAFR Nov 12 '21 at 14:34
  • Shouldn't it then be a `char[][]` instead? Anyway, you'll need to cycle through every character in `line` with another `for` loop (from 0 to `line.length`) and, supposing the inner loop uses `j` as an index, assign the character (single-character String?) to lecture_tab[i][j]. – Federico klez Culloca Nov 12 '21 at 14:37
  • If you are trying to split the line at each char then just do `lecture_tab[i] = line.split("");` – Eritrean Nov 12 '21 at 14:37
  • Does this answer your question? [Read text file into an array](https://stackoverflow.com/questions/10257981/read-text-file-into-an-array) – Arsh Coder Nov 12 '21 at 14:50

3 Answers3

0

The issue is when you do:

 this.lecture_tab[i] = line;

line is a String, and this.lecture_tab[i] is a String[].

If you want to have a separate character in each of your String, you can use :


public void readLines() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("lvl1.txt"));

        int i = 0;
        while (i < 15) {
            for (String line; (line = br.readLine()) != null; ) {
                System.out.println(line);
                for(int j = 0; j < line.lenght(); j++) {
                     this.lecture_tab[i][j] = "" + line.charAt(j);
                }
                i += 1;
            }
        }
        System.out.println(lecture_tab);
    }

In this case, I would recommand to change lecture_tab to an 2D array of char (char[][] lecture_tab;)

obourgain
  • 8,856
  • 6
  • 42
  • 57
  • Thanks for ur feedback which has helped me a lot. Everything seems to have worked but when I call my method to display my matrix it only displays the first row, the rest is "null" ```public void readLines() throws IOException { BufferedReader br = new BufferedReader(new FileReader("lvl1.txt")); String line; for (int i = 0; i < 15; i++) { while ((line = br.readLine()) != null) { for(int j = 0; j < line.length(); j++) { this.lecture_tab[i][j] = line.charAt(j); } } } } ``` – RandAFR Nov 12 '21 at 15:35
  • Sorry, I don't know how to add a comment properly... – RandAFR Nov 12 '21 at 15:36
0

You can use an array of chars if what you want in each "cell" is a single character. You can also take advantage of the toCharArray(String) method to do the conversion.

You need something like this:

    public static void main(String[] args) throws IOException {

        char[][] lecture_tab = new char[15][];
        BufferedReader br = new BufferedReader(new FileReader("lvl1.txt"));

        int i = 0;
        while (i < 15) {
            for (String line; (line = br.readLine()) != null; ) {
                System.out.println(line);
                lecture_tab[i] = line.toCharArray();
                i += 1;
            }
        }
        System.out.println(lecture_tab);
    }

Federico Nafria
  • 1,397
  • 14
  • 39
0

You can use this:-

BufferedReader abc = new BufferedReader(new FileReader(myfile));
List<String> lines = new ArrayList<String>();

while((String line = abc.readLine()) != null) {
    lines.add(line);
    System.out.println(data);
}
abc.close();

// If you want to convert to a String[]
String[] data = lines.toArray(new String[]{});
Arsh Coder
  • 688
  • 9
  • 33