Results 1 to 2 of 2

Thread: reading Matrix

  1. #1
    metrokid is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default 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'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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by metrokid View Post
    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 {
    		
    		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):

    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);
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Matrix class
    By maz22 in forum New To Java
    Replies: 5
    Last Post: 10-15-2010, 06:53 AM
  2. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  3. scalability of matrix
    By ajay kumar in forum New To Java
    Replies: 1
    Last Post: 12-07-2009, 08:21 AM
  4. displaying 2D-Matrix
    By srinivasmallabathula in forum New To Java
    Replies: 2
    Last Post: 02-18-2009, 07:19 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

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