Results 1 to 3 of 3
  1. #1
    pooja123 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default how to read a text delimited file using 2 dimentional array in java ??

    hi,

    I am new to java programming.. I have to do a task where in i have to read a text delimeted file in an array.. For example.. If the file is as follows

    Name place Value
    adi goa 20
    shri mumbai 30
    riya bangalr 45

    I want it to be read in java so as to get an array[row][columns]
    This is something i am currently upto, but cant get any further.

    import java.io.BufferedReader;
    import java.io.FileReader;

    public class generateGML{
    public static void main(String[] argv)
    throws Exception{
    BufferedReader fh = new BufferedReader(new FileReader("miRTarbase.txt"));
    String s;
    while ((s=fh.readLine())!=null){
    String[] columns = s.split("\t");
    String name = columns[0];
    String place = columns[1];
    String value = columns[2];

    It reads columns,But I want it two dimentionally,as in something like matrix[row_num][column_num].

    Can anyone please suggest me..

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    use ArrayList because you don't know length of file / size of array required

    Java Code:
    java.util.List<String> textFile = new java.util.ArrayList<String>();
    while ((s=fh.readLine())!=null) {
        textFile.add(s);
    }

    then copy to a 2 dimensional array now that you know the size
    Java Code:
    int rows = textFile.size();
    String[][] matrix = new String[][rows];
    
    //to retrieve element from ArrayList where 'i' is the index required like array[i]
    String col = textFile.get(i); //returns element
    String[] col = textFile.get(i).split("\t"); //returns processed element

  3. #3
    pooja123 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 11-01-2010, 02:04 PM
  2. Replies: 3
    Last Post: 09-24-2010, 08:53 PM
  3. adding elemnts of two dimentional array into vector
    By sara12345 in forum New To Java
    Replies: 10
    Last Post: 12-31-2009, 11:07 AM
  4. How can we read text file in java 1.4
    By javasaravanan in forum Advanced Java
    Replies: 4
    Last Post: 11-30-2009, 03:28 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •