Results 1 to 2 of 2
- 01-09-2012, 09:26 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
put data from txt into 2dim-array?
Hey there,
still trying to get some data from text-file into an 2dim-array, but cannot get it to work.
well, maybe I can, but I just can't figure out how split-method exactly works.
What I've got so far:
my text-file looks like this:Java Code:import java.io.*; public class Felder{ public static void main(String args[]) throws Exception{ String felder = "/afs/informatik.uni-goettingen.de/user/f/f.luethe/Downloads/felder.txt"; FileReader freader = new FileReader(felder); BufferedReader breader = new BufferedReader(freader); String zeile; String[] array = new String[531]; String[][] all; int i; for (i=0; (zeile = breader.readLine()) != null; i++) { array[i] = zeile; System.out.println(array[i]); } breader.close(); } }
...
541 TNFSF18 TNF family Q9UNG2 ENST00000404377
128 pancreatic polypeptide NPY family P01298 ENST00000225992
145 CGRP2 calcitonin family P10092 ENST00000396377
...
at the end of it, i want an 2dim array that looks like this:
all[0][0] = 541
all[0][1] = TNFSF18
...
all[0][4] = ENST00000404377
all[1][0] = 128
...
and so on
now, where it says "array[i] = zeile" the line after that I would split up the i-th line in "array",
but I don't know how to ..store it in the 2dim array.
May someone be able to explain?
The thing is: I know that split does as it says.. array.split("\t"); - splits it up where the tabs are.. this is what I want!
but what I do NOT know is, how it.. well how I can put it into the other array.
I hope I could explain well enough!?
EDIT:
I've uploaded the wrong - my old - code..
somehow got really confused and didn't even realize when I was commenting on it..
realized it now, though, when I tried to work through it again.
anyway this is the new one:
It works out quite well, but still not entirely. If I do it this way, I'm only getting an array with "null" in every line and every column...Java Code:import java.io.*; import java.lang.*; import java.util.*; public class Felder{ public static void main(String args[]) throws Exception{ String felder = "/afs/informatik.uni-goettingen.de/user/f/f.luethe/Downloads/felder.txt"; FileReader freader = new FileReader(felder); BufferedReader breader = new BufferedReader(freader); String zeile; String[] array = new String[531]; String array3; String[][] all = new String[array.length][5]; int i; for (i=0; (zeile = breader.readLine()) != null; i++) { array[i] = zeile; array3 = Arrays.toString(array); String[] array2 = array3.split("\t"); System.out.println(Arrays.toString(array2)); //System.out.println(Arrays.toString(array2)); //array.split("\t"); // for(int j=0; j<5; j++){ // all[i][j] = array2[i++]; // System.out.println(all[i][j]); //} } breader.close(); } }
but when I print the arrays out in the first for-loop, it seems to be right. so the storing or saving or whatever it is in the 2dim array
doesn't work.
I didn't want to override the old - first posted - code, b/c maybe you can understand it better this way. hope that's okay.Last edited by cups; 01-09-2012 at 10:28 AM.
- 01-09-2012, 11:40 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Editing array data
By silverspoon34 in forum New To JavaReplies: 4Last Post: 04-15-2011, 09:20 PM -
Receive data from an array
By phixion in forum New To JavaReplies: 6Last Post: 01-27-2011, 12:50 AM -
Array overwriting data
By hobo in forum New To JavaReplies: 7Last Post: 10-27-2010, 01:29 PM -
add data into an array
By mispeed in forum New To JavaReplies: 9Last Post: 11-08-2007, 03:53 AM -
Add data to an array
By adlb1300 in forum New To JavaReplies: 8Last Post: 11-05-2007, 02:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks