954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

2d array problem

What is wrong with this code. when it do system.out.println(matrix[0][0] i should get 1 char i get the entire line.

int r =Integer.parseInt(line.substring(0));
            int c = 10;
            String[][] matrix = new String[r][c];
            
        while (line !=null)
        {
            for(int i =0; i< r; i++)
            {
             
                for (int j =0; j< c; j++)
                {
                    line = br.readLine();
                    matrix[i][j] = line;
                }
            }
        }
        
            System.out.print(matrix[0][1]);
        }
mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

Well, go figure:

line = br.readLine();
matrix[i][j] = line;

If you put the whole line in there, that's what you're going to get.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

so what function can i use to read in a single char at a time?

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

read the line, and within the loop, you just extract a single char from that line.
that's the one you put in your array, next loop, you read the next char and so on, till you've reached the end of the line

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You