Home > database >  Convert a txt file to a String[][]
Convert a txt file to a String[][]

Time:11-13

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 :

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














CodePudding user response:

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;)

CodePudding user response:

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);
    }

CodePudding user response:

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[]{});
  • Related