Results 1 to 9 of 9
Thread: Parse file into arrayList
- 05-02-2011, 12:42 AM #1
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:
I want to store in arrayList the numbers and the prefix. I ve already read it with the following code: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 didnt manage to store it in the arrayList. Any help would be appreciated!!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();
- 05-02-2011, 12:45 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 05-02-2011, 01:12 AM #3
: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.
- 05-02-2011, 01:23 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-02-2011, 01:49 AM #5
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);
- 05-02-2011, 02:11 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 05-02-2011, 02:20 AM #7
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''.
- 05-02-2011, 02:25 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You should do something like this
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.Java Code:String[] input = line.split(" ");
- 05-02-2011, 08:01 AM #9
Similar Threads
-
How to parse a .db file ?
By aminov in forum JDBCReplies: 0Last Post: 08-06-2009, 04:11 PM -
Parse Arraylist to Byte
By Deepa in forum New To JavaReplies: 1Last Post: 03-13-2009, 12:44 PM -
Parse EDI File Using java
By vaskarbasak in forum Advanced JavaReplies: 6Last Post: 09-24-2008, 02:38 PM -
How to parse the CSV(Comma separation values)file and validate the file using java
By padmajap13 in forum Advanced JavaReplies: 7Last Post: 05-23-2008, 03:46 AM -
how to parse an xml file
By oregon in forum XMLReplies: 3Last Post: 08-01-2007, 04:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks