Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: How to perform row operation in data??

  1. #1
    ranjeet270 is offline Member
    Join Date
    Aug 2014
    Posts
    3
    Rep Power
    0

    Default How to perform row operation in data??

    Hello all ! I am able to perform column operation but not able to perform row operation because i am not able to store data say a 2 matrix [][]. I need help to store the data into a 2-D matrix. Here is my code:
    Java Code:
    import java.awt.List;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Scanner;
    public class colRowRead {
    
    	public static void main(String[] args) throws Exception {
    		// TODO Auto-generated method stub
    
    		BufferedReader bf = new BufferedReader(new FileReader("col.txt"));
    		String s;
    		
    		String[] columns =null;
    		ArrayList<String> textFile = new ArrayList<String>();
    		while ((s=bf.readLine())!=null) {
    		   textFile.add(s);
    		  columns = s.split(" ");
    		  System.out.println(columns[1]);  //This will show column 2
    		}
    	
    		int rows = textFile.size();  //Number of rows
    		int cols = columns.length;   // Number of columns
    	    
    	    
    	    String[][] matrix = new String[rows][cols];   // I want to store the data into Matrix [rows][column] and not able to do so?
    		
    	    for(int i=0; i<rows; i++) {	
    		String col = textFile.get(i);
    		String[] col1 = textFile.get(i).split(" ");
    		System.out.println(col);  // Display the all data
    		System.out.println(col1 [3]); //Display the forth column
    		}
    	} 
    }
    
    
    /*	I tried something like that:
            for(int i=0; i<rows; i++) {	
    	   for(int j=0; j<cols; j++){
    		matrix [i][j]=textFile.get(i).split(" ");
    		//System.out.println(matrix[i][j]);
    		  }
    	}
    	 */
    File col.txt is like this:
    Java Code:
     
    5 9 7 1 5
    3 6 8 6 8
    4 6 7 8 9
    9 8 3 5 7

    Thank you :)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to perform row operation in data??

    There are several ways. One is to have an inner loop for columns and assign each column for row "i" the appropriate value.
    The easy way, imo, is to simply take the returned array from the split operation and assign it to row "i". A 2D array is nothing
    more than an array of arrays.

    Regards,
    Jim
    ranjeet270 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ranjeet270 is offline Member
    Join Date
    Aug 2014
    Posts
    3
    Rep Power
    0

    Default Re: How to perform row operation in data??

    Thank you replying..:)

    But I didn't get. could you write code, please? I'm very beginner in programming.

  4. #4
    Hamenopi is offline Nub
    Join Date
    Sep 2014
    Location
    Houston
    Posts
    8
    Rep Power
    0

    Default Re: How to perform row operation in data??

    Quote Originally Posted by ranjeet270 View Post
    Thank you replying..:)

    But I didn't get. could you write code, please? I'm very beginner in programming.
    Jim pretty much did that in verbose psuedo code.

  5. #5
    ranjeet270 is offline Member
    Join Date
    Aug 2014
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 04-19-2011, 10:00 AM
  2. Replies: 11
    Last Post: 03-02-2011, 05:33 AM
  3. Replies: 1
    Last Post: 03-17-2010, 09:41 AM
  4. Cannot perform a PING
    By mnementh64 in forum Networking
    Replies: 1
    Last Post: 09-17-2009, 05:39 PM
  5. Replies: 6
    Last Post: 04-20-2009, 02:46 AM

Tags for this Thread

Posting Permissions

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