• 12-03-2010, 11:35 PM
metrokid
Hi.

I have encountered a big problem.

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[0].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.
• 12-04-2010, 12:48 PM
JosAH
Quote:

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:

Code:

```        private static int[] readMatrixLine(int n, BufferedReader br) throws IOException {                                 String line= br.readLine();                                 if (line == null)                         throw new IOException("incorrect matrix row");                                 Scanner s= new Scanner(new StringReader(line));                 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):

Code:

```                int[][] matrix= new int[3][];                                 BufferedReader br= new BufferedReader(new InputStreamReader(System.in));                                 for (int row= 0; row < matrix.length; row++)                         matrix[row]= readMatrixLine(3, br);```
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