Results 1 to 9 of 9
Thread: Read File into 2d array
- 03-23-2010, 09:40 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
Read File into 2d array
Hi!
Yepp i´m a beginner to java and I face a propably simple problem. I did some research on google but couldn´t find a solution for my problem. I would appreciate your help on that.
Problem:
Reading from a plain txt file into an double array
In the original code the array was defined as follows:
double[][] matrix = {{0,2.3,3.1,2.1 ... },
{2,3,0,1.3,1.3 ... },
{... }};
What I want to do now is to read the data from a file instead of providing it directly in the code (there was a 65k something limitation dropped).
File format text file
0,2.3,3.1,2.1 ... Linebreak
2,3,0,1.3,1.3 ... Linebreak
...
..
The code if experimented with:
I found the above example on this website. There are some issues now with data types. I would appreciate your support for this problem.Java Code:BufferedReader br = null; try { br = new BufferedReader(new FileReader("distancematrix_edited_for_import.txt")); String line = null; while ((line = br.readLine()) != null) { //String[] values = line.split(","); double[][] matrix = line.split(","); //Do necessary work with the values, here we just print them out for (String str : values) { System.out.println(str); } System.out.println(); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } }
- 03-23-2010, 10:02 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The line double[][] matrix = line.split(","); won't compile because the String.split returns a String[] and you are trying to assign it to a double array. You'd have to convert each string in the String[] to a double using the Double.parseDouble method.
- 03-23-2010, 10:07 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
implemtation follows...
...i´m trying to implement that.
- 03-23-2010, 10:12 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Well then you start with a double array, initialize it to the length of the string array.
Then you use a loop which grabs a string from the string array, converts it to a double and sets the double value into the double array.
- 03-23-2010, 10:56 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
almost there...
I did this and I´m almost there: thx for the support!
Java Code:BufferedReader br = null; double[][] matrix = new double[102][102]; try { br = new BufferedReader(new FileReader("distancematrix_edited_for_import.txt")); String line = null; int x=0; int y=0; while ((line = br.readLine()) != null) { String[] values = line.split(","); for (String str : values) { double str_double = Double.parseDouble(str); matrix[x][y]=str_double; System.out.println(str_double); } y=y+1; } x=x+1; }
- 03-23-2010, 11:03 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Well done!
- 03-23-2010, 11:10 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
I´m also a bit proud of myself ^^
Works perfect now. Awesome!
- 03-23-2010, 02:37 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Sorry to spoil the fun but this:
... doesn't really work: for all the values on the line the same matrix entry m[x,y] is assigned. Those loop increments are at the wrong place.Java Code:while ((line = br.readLine()) != null) { String[] values = line.split(","); for (String str : values) { double str_double = Double.parseDouble(str); matrix[x][y]=str_double; System.out.println(str_double); } y=y+1; } x=x+1; }
kind regards,
Jos
- 03-23-2010, 02:55 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM -
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06-10-2008, 03:53 AM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks