How do I take a text file and turn it into a 2D array in Java?

I have a text file, it is as follows:

1 1 1 0
0 0 1 0
0 0 1 0
0 9 1 0

I want to read this and turn it into an 2D array line by line. First I used BufferedReader and FileReader, then turned them into one-dimensional arrays. I want to add my one-dimensional arrays to be added to my 2D array. Here is my code:

BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line;
char[][] maze = new char[8][8];
            
while ((line = br.readLine() ) != null ){
                
    char[] row = line.toCharArray();
    int x = 0;
    for (int i = 0; i < row.length; i++) {
        maze[x][i] = row[i];
                
        System.out.print(maze[i]);
        System.out.printf("%n");
        x++;
    }
}

I am trying to get a 2D array because I am going to check coordinates later on. So I want my 2D array's rows to be determined by every line of the text file I have.
But the output I get is the following:

1
  
  1
    
    1
     
      0
0

  0
  
    1
    
      0
0
 
  0 

    1

      0
0

  9

    1

      0

What am I doing wrong?

Upvotes: 3

Views: 1303

Answers (3)

Siddharth Yadav
Siddharth Yadav

Reputation: 1

BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
char[][] arr = new char[4][4];
int i,j;

for (i=0; i<4; i++) {
    String[] str1=br.readLine().split(" ");
    for (j=0; j<4; j++) {
        arr[i][j] = str1[j].charAt(0);
    }
}

for (i=0; i<4; i++) {
    for (j=0; j < 4; j++) {
        System.out.print(arr[i][j]+" ");
    }
    System.out.println();
}

Upvotes: 0

Apurva
Apurva

Reputation: 7901

You should put System.out.printf("%n"); out side the for loop. As it's inside the for loop, it prints a new line after printing every character.

It should be like,

while ((line = br.readLine() ) != null ){

    char[] row = line.toCharArray();
        int x = 0;
        for (int i = 0; i < row.length; i++) {
        maze[x][i] = row[i];

        System.out.print(maze[i]);
        x++;
        }
        System.out.printf("%n");  //mention this
    }

And one more thing, increment of x will not affect the sequence of output.

Upvotes: 1

trooper
trooper

Reputation: 4512

One issue is that you are incrementing x within your for loop, and resetting its value to 0 with each iteration of the while loop. Since you are using this variable to count rows, x++ actually belongs outside of the for loop (but still within the while), and its initialization to 0 belongs before the start of the while loop.

A similar issue exists with this statement, System.out.printf("%n");. You are printing this for every iteration of the for loop, so you are getting a new line between every character. Same as x above, move this statement outside the for loop (but still within the while).

     int x = 0;
     while ((line = br.readLine() ) != null )
     {
         char[] row = line.toCharArray();
         for (int i = 0; i < row.length; i++) 
         { 
             maze[x][i] = row[i];
             System.out.print(maze[i]);
         }
         System.out.printf("%n");
         x++;
    }

Upvotes: 0

Related Questions