Results 1 to 9 of 9
  1. #1
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Parse file into arrayList

    Hello there and thanks in advance for your help. I have got .obj file and i want to open it and parse it to a arrayList. The file has the following format:

    Java Code:
    # produced by mview OBJ exporter
    # 
    
    g main
    v -0.021198 0.127200 0.009153
    v -0.014447 0.128935 0.009035
    f 1316 1041 1319
    f 1318 1316 1319
    f 77 109 1010
    I want to store in arrayList the numbers and the prefix. I ve already read it with the following code:

    Java Code:
    FileInputStream fstream = new FileInputStream("lagos.obj");
              // Get the object of DataInputStream
              DataInputStream in = new DataInputStream(fstream);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
              //Read File Line By Line
              while ((strLine = br.readLine()) != null) {
                  // Print the content on the console
                  System.out.println(strLine);
    
              ArrayList<ArrayList<String>> array;
              array = new ArrayList<ArrayList<String>>();
              }
              
              System.out.print(strLine);
              //Close the input stream
              in.close();
    I didnt manage to store it in the arrayList. Any help would be appreciated!!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's fairly simple. Before the line with the while loop, declare the array list. Then as you go through the loop, add strLine to the array list.

    Java Code:
    ArrayList<String> array = new ArrayList<String>();
    loop
      split line
      loop
        store each item in array list
      end loop
    end loop

  3. #3
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    :D yes i got it. In that way I have a 2d array list. I can have access with array.get(1) to the line but how can I have access to the elements of the line?

    By the way thanks for the help.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You stored it in a 2d array list, so each element in the array list IS an array list.

    if you have the following declaration
    Java Code:
    ArrayList<ArrayList<String>> info = new ArrayList<ArrayList<String>>();
    info.get(0); //this returns an array list, which you can then search as well.
    //can be written like this
    ArrayList<String> piece = info.get(0);
    //then you can search the second part
    piece.get(0); //returns item located at 0, 0.

  5. #5
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    I ve tried this but it doesnt work out:

    Java Code:
                ArrayList<ArrayList<String>> array1 = new ArrayList<ArrayList<String>>();
    
    
                for(int i=1; i<5768; i++){
                   ArrayList<String> array = new ArrayList<String>();
    
                  while ((strLine = br.readLine()) != null) {
    
                      //ArrayList file =
                      array.add(strLine);
    
              }
    
                 array1.add(i, array);
    
              }
               
             System.out.println(array1);

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens? Errors? If so, please post them as they appear. Or is it a logical problem?

    To understand, your goal is to store each line of the file in it's own array list?

  7. #7
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    with that code:


    Java Code:
    ArrayList<String> array = new ArrayList<String>();
    
                  while ((strLine = br.readLine()) != null) {
                      array.add(i,strLine);
              }
              }

    you have all the file in one single dim arrayLIst. But now i do not have access to a specific element of the file but only to specific lines of the file (for example array.get(1) corresponds to [v -0.021198 0.127200 0.009153]). I want something like array.get(1).get(1) = ''v''.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You should do something like this
    Java Code:
    String[] input = line.split(" ");
    This will produce an array of x elements, split based on spaces. Then you can loop through the array, adding each element to the array list. Finally, you can add the array list to the main array list.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    Or by coding to the interface, you can use the Arrays.asList(...) method.
    Java Code:
    List<List<String>> list = new ArrayList<List<String>>();
    :
    :
    list.add(Arrays.asList(line.split(" ")));
    db

Similar Threads

  1. How to parse a .db file ?
    By aminov in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 05:11 PM
  2. Parse Arraylist to Byte
    By Deepa in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 01:44 PM
  3. Parse EDI File Using java
    By vaskarbasak in forum Advanced Java
    Replies: 6
    Last Post: 09-24-2008, 03:38 PM
  4. Replies: 7
    Last Post: 05-23-2008, 04:46 AM
  5. how to parse an xml file
    By oregon in forum XML
    Replies: 3
    Last Post: 08-01-2007, 05:53 PM

Posting Permissions

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