1. Member Join Date
Dec 2010
Posts
1
Rep Power
0

## reading Matrix

Hi.

I have encountered a big problem.

PHP Code:
```import java.util.Scanner;

public class t2 {

public static void main(String [] args) {

Scanner scn = new Scanner(System.in);

System.out.print("Matrix Size: ");
int dimensao = lerInteiro(scn, 0, 0, 1);
int[][] matrix = new int[dimensao][dimensao];
matrix = lerArray(scn, dimensao, 0, 0);

}

private static int[][] lerArray(Scanner scn, int dim, int i, int j) {

int[][] input = new int[dim][dim];

System.out.print("Insert line number " + i + ": ");
for(j=j; j < input.length; j++) {
input[i][j] = lerInteiro(scn, i, j, dim);
}
i++;
j = 0;
if(input.length < dim) {
lerArray(scn,dim,i,j);
}

return input;
}

private static int lerInteiro(Scanner scn, int i, int j, int dim) {

String[] linha = new String[dim];

linha[i] = scn.nextLine();
String[] inteiro = linha[i].split(" ");

return Integer.parseInt(inteiro[j]);
}
}```
I'm not being able to read user input of the matrix. The user has to input the matrix line by line with a space between each coordenate.

I have setup the method 'lerInteiro' to read the whole line and split it in an array.

Then I would want to have the method 'lerArray' to go fill up the input array, with the numbers that are being returned by the 'lerInteiro' method.

So I don't know how to write the code in 'lerArray' method.

The first dimension of the array are the lines and second are the columns.
I want it to go trough each column and then change lines, and go trough each column again and on and on.

If anyone could help I would be very aprecciated.

Sorry for some things that are not in english.

Hope I was clear enough.
Last edited by metrokid; 12-04-2010 at 02:04 AM.  Reply With Quote

2. ##  Originally Posted by metrokid I'm not being able to read user input of the matrix. The user has to input the matrix line by line with a space between each coordenate.

I have setup the method 'lerInteiro' to read the whole line and split it in an array.

Then I would want to have the method 'lerArray' to go fill up the input array, with the numbers that are being returned by the 'lerInteiro' method.

So I don't know how to write the code in 'lerArray' method.

The first dimension of the array are the lines and second are the columns.
I want it to go trough each column and then change lines, and go trough each column again and on and on.

If anyone could help I would be very aprecciated.
Don't do it that way; read entire lines and use each line to fill in complete rows of your matrix. A Scanner together with a StringReader can do the job:

Java Code:
```	private static int[] readMatrixLine(int n, BufferedReader br) throws IOException {

if (line == null)
throw new IOException("incorrect matrix row");

int[] row= new int[n];

for (int i= 0; i < n; i++) {
if (s.hasNextInt())
row[i]= s.nextInt();
else
throw new IOException("matrix row too short");
}
if (s.hasNextInt())
throw new IOException("matrix row too long");

return row;
}```
Given a size n for the number of columns in a row and a BufferedReader (used to read a single line) the above method returns an int array that can be used for your matrix. If anything is wrong an IOException is thrown.

An entire matrix can be read as follows (from System.in here):

Java Code:
```		int[][] matrix= new int[];

for (int row= 0; row < matrix.length; row++)
The rows of a matrix are allocated and a square matrix is read in. I realize this was a bit of spoonfeeding but I hope you'll study the code and not turn it all in as if it were yours.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•