Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    I have moved this post to a more appropriate forum.

    First off, are you sure you want to use a 2D array for this? It seems like you want to use a one-dimensional array (or List) that contains Objects that hold the name, place, value information.

    But if you want to use a 2D array, just think of it as an array of arrays. You get an array from the split method, so you just have to add that array to another array holding those results. Does that make sense?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    well yes i get what you are saying and it does make sense.. so i have found something like

    File input = new File(argv[0]);
    BufferedReader br = new BufferedReader(new FileReader(input));
    String line;

    List<List<String>> rows = new ArrayList<List<String>>();

    while ((line = br.readLine()) != null) {
    String[] columns = line.split("\t");
    List<String> columnList = Arrays.asList(columns);
    rows.add(columnList);
    }

    but the last like here "rows.add(columnList) g
    ives me an error.
    And also that i am a little confused with the list of list.. Is it that the 1st list separates columns and the second list separates rows ? and also that what is the "row" and "columnList" doing here ?? It would be easy for me if i get a small explanation for the above code as to how it works.. Thank you in advance..
    Last edited by pooja123; April 1st, 2011 at 09:15 AM.

Similar Threads

  1. How to read a text from an Image file?
    By GautamRy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 5th, 2011, 05:02 AM
  2. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  3. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  4. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM