Results 1 to 2 of 2
  1. #1
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default 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:

    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();
    
    
    	
    	}
    }
    my text-file looks like this:
    ...
    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:
    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();
    
    
    	
    	}
    }
    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...
    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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: put data from txt into 2dim-array?

    I'd put the result of each line.split( ... ) in a List<String[]>; when reading and splitting is done I'd create a two dimensional array from the List<String>.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Editing array data
    By silverspoon34 in forum New To Java
    Replies: 4
    Last Post: 04-15-2011, 09:20 PM
  2. Receive data from an array
    By phixion in forum New To Java
    Replies: 6
    Last Post: 01-27-2011, 12:50 AM
  3. Array overwriting data
    By hobo in forum New To Java
    Replies: 7
    Last Post: 10-27-2010, 01:29 PM
  4. add data into an array
    By mispeed in forum New To Java
    Replies: 9
    Last Post: 11-08-2007, 03:53 AM
  5. Add data to an array
    By adlb1300 in forum New To Java
    Replies: 8
    Last Post: 11-05-2007, 02:01 AM

Posting Permissions

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