Results 1 to 2 of 2
Thread: reading Matrix
 12032010, 11:35 PM #1Member
 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[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 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; 12042010 at 02:04 AM.
 12042010, 12:48 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
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 { 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; }
An entire matrix can be read as follows (from System.in here):
Java 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);
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

Matrix class
By maz22 in forum New To JavaReplies: 5Last Post: 10152010, 06:53 AM 
help in matrix
By Engineer in forum New To JavaReplies: 7Last Post: 10062010, 01:26 PM 
scalability of matrix
By ajay kumar in forum New To JavaReplies: 1Last Post: 12072009, 08:21 AM 
displaying 2DMatrix
By srinivasmallabathula in forum New To JavaReplies: 2Last Post: 02182009, 07:19 PM 
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:37 AM
Bookmarks