#BufferedReader

1 messages · Page 1 of 1 (latest)

dusk vortex
#

I want my code to convert my .txt into a 2D array, however for some reason it only seems to take the last line and convert to array, then distribute to all the other indexes. Here is my code:

the java file is linked (it was too long)

here is my txt:

3
0 2 0
1 0 3
1 2 4
cloud treeBOT
cloud treeBOT
#

<@&987246717831381062> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        String[][] array = new String[3][3];
        try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
            String line;
            int row = 0;
            while ((line = br.readLine()) != null) {
                String[] values = line.split(",");
                for (int col = 0; col < values.length; col++) {
                    array[row][col] = values[col];
                }
                row++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Print the array
        for (String[] row : array) {
            for (String value : row) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
}

Make sure you have a file named "input.txt" in the same directory as your Java file, and it should contain the data you want to convert into a 2D array. Each line in the file should represent a row in the array, and values should be separated by commas.

For example, if your "input.txt" file contains:

1,2,3
4,5,6
7,8,9

The output will be:

1 2 3 
4 5 6 
7 8 9 
dusk vortex
#

um... well that worked