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

    Default FileReader and calling a method which gives back an array

    Hey there,

    I know the title is stupid, but I didn't know how else to call ist.
    I'm trying to write a method that reads from a file.
    The file contains of 5 rows and a lot of columns. I wanted to read in the 5 rows into an array.
    As I only new the method readLine() but nothing of rows, I've tried this my own:

    Java Code:
    import java.io.*;
    
    public class PepHormon{
    	public String[] einlesen(){
    		
    		
    		String data = "";
    		String[] dataArray_counter = new String[10];
    		String[] EndonetID; // 0
    		String[] Hauptname; // 1
    		String[] Hormonklasse; // 2
    		String[] Swissprot; // 3
    		String[] EnsembleID; // 4
    		int i;
    
    		int counter = 0;
    		
    		// s1 endonetid, s2 hauptname, s3 hormonklasse, s4 swissprot id, s5 ensembl id
    		
    		  
    	try{
    	BufferedReader reader = new BufferedReader(new FileReader("/home/lisa/Downloads/peptidhormone.txt"));
    		for(counter = 0; counter<5; counter++){
    			for(i=0; i<dataArray_counter.length; i++){
    				while(data != null){
    					data = data+reader.read();
    				}
    			dataArray_counter[i] = data;
    			}
    		}
    		} catch( FileNotFoundException filenotfoundexxption ) {
          System.out.println( "myFile.txt, does not exist" );
       } catch( IOException ioexception )	{
          ioexception.printStackTrace( );	
    		}
    	return dataArray_counter;
    	}
    }
    and b/c I'm really really sure, that I'm not nearly done, I wrote a class to test this one:

    Java Code:
    class TestPeph{
    	public static void main(String[] args){
    		
    		PepHormon tryit = new PepHormon();
    		
    		System.out.println(tryit.einlesen());
    
    	}
    }
    but if I try to run the test class, nothing happens.
    Is this, b/c I called the other method wrong?
    Or is it just b/c of the other method, b/c I know it won't entirely work yet,
    I just wanted to see what was happening.. trial and error style ;)

    Can someone maybe help?

    x cups

  2. #2
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: FileReader and calling a method which gives back an array

    Can you explain how the data is stored on the file, because by definition of a row, if you use the readline method, you will be reading a row.

    Furthermore, to distinguish different data contents on the same line, you should set some sort of special character...take a look at this example of how a file should be stored:

    *name*age*home*
    *name*age*home*
    *name*age*home*

    By using the readline method, you will be reading a whole record.....you then must make use of the special characters to split the record into 3 parts; the name, age and home... This is just an example... dont know if Im answering your question... If i didnt please explain further what is your problem

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: FileReader and calling a method which gives back an array

    if I try to run the test class, nothing happens.
    Add some println statements to your code to show the execution flow and the values of variables as the code executes.
    Copy and paste here the contents of the console when you execute the code if you have problems.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  4. #4
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: FileReader and calling a method which gives back an array

    Hey,

    thanks for the fast reply.

    The data is stored like this:

    name;ensembleID;swissprotID
    name;ensembleID;swissprotID
    name;ensembleID;swissprotID
    ...
    name;ensembleID;swissprotID


    I don't know the other 2 right now, but they're seperated by ";"
    This is emberassing now, but I've confused row and column (I'm german and I confused the translation.. sorry about that).

    If I would read a line, I would have arrays that contain the name of a hormone aswell as its swissprot ID and ensembleID etc.
    But what I want is that one array contains all the names, and one all the swissprot IDs etc.
    Then, if I need the ID of an certain hormone, I know in which array it is, and which index my hormone has (which then is the same index as it is for the IDs).

    I hope I explained my problem better this time.. or at least didn't use some word that means the opposite of what I meant ;)

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: FileReader and calling a method which gives back an array

    After you read a line from the file into a String, use the String class's split method to separate the parts of the String into separate Strings on the ; boundaries. The line from the file is a row and the split parts of the line are the columns.

  6. #6
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: FileReader and calling a method which gives back an array

    Yes do it like Norm said, by using the String class. Otherwise, if you feel up for the challenge you could also make use of some regular expression which can match some patterns. This is a bit harder to understand if you never tried them before. The use of the String class is much easier. =)

  7. #7
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: FileReader and calling a method which gives back an array

    Okay so I've tried anew.
    But if I want either return the array or print the array it says "cannot find symbol":

    Java Code:
    PepHormon.java:31: cannot find symbol
    symbol  : variable data
    location: class PepHormon
    	System.println(data[1]);
    	               ^
    1 error
    this is my code:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class PepHormon{
    	public String einlesen(){
    		
    		try{
    		BufferedReader reader = new BufferedReader(new FileReader("/home/lisa/Downloads/peptidhormone.txt"));
    		String data = reader.readLine();
    		String[] dataArray_counter = new String[10];
    		String[] EndonetID; // 0
    		String[] Hauptname; // 1
    		String[] Hormonklasse; // 2
    		String[] Swissprot; // 3
    		String[] EnsembleID; // 4
    		int i;
    
    		int counter = 0;
    		
    		
    		while(data != null){
    			data.split(";");		
    		}
    
    	} catch( FileNotFoundException filenotfoundexxption ) {
          System.out.println( "myFile.txt, does not exist" );
       } catch( IOException ioexception )	{
          ioexception.printStackTrace( );	
    		}
    
    	System.println(data[2]);
    }
    }
    I tried to print out just one position in the array, just to see if it works, but it doesn't.
    why can't if find my variable anymore?
    I thought I had just made an array out of it?!

    I tried another way, b/c I wasn't sure which was right:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class PepHormon{
    	public void einlesen(){
    		
    		
    		BufferedReader reader = new BufferedReader(new FileReader("/home/lisa/Downloads/peptidhormone.txt"));
    		String data = reader.readLine();
    		String[] data_array = data.split(";");
    		String[] dataArray_counter = new String[10];
    		String[] EndonetID; // 0
    		String[] Hauptname; // 1
    		String[] Hormonklasse; // 2
    		String[] Swissprot; // 3
    		String[] EnsembleID; // 4
    		int i = 0;
    		
    		int counter = 0;
    		for(i=0; i<10; i++){
    		System.println(data_array[i]);
    		}	
    	}	
    }
    but now it says
    Java Code:
    PepHormon.java:21: cannot find symbol
    symbol  : method println(java.lang.String)
    location: class java.lang.System
    		System.println(data_array[i]);
    		      ^
    1 error
    I don't understand whats going on. Why can it always not find some symbol?!
    Last edited by cups; 12-03-2011 at 02:43 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: FileReader and calling a method which gives back an array

    System.println(data[1]);
    The variable: data is not an array so you can not use the [1] syntax with it.

    Also there is no println() method in the System class. Check the syntax for using the println method.
    Last edited by Norm; 12-03-2011 at 04:20 PM.

  9. #9
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: FileReader and calling a method which gives back an array

    Aww, sorry! I got so confused and couldn't get anything to work, so I asked too fast.
    It prints it out now.
    Thanks for helping, I'll try to go on myself.

  10. #10
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: FileReader and calling a method which gives back an array

    Hey!

    I don't know what happened, but I'm getting a

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at PepHormon.einlesen(PepHormon.java:23)
    	at TestPeph.main(TestPeph.java:7)
    -error!

    I don't know why.
    It's printing out exactly what I want, and then the Exeption.
    Somethings wrong, but I cannot figure out what it is.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class PepHormon{
    	public void einlesen(){
    		
    		try{
    		BufferedReader reader = new BufferedReader(new FileReader("/home/lisa/Downloads/peptidhormone.txt"));
    		String data = reader.readLine();
    		String[] data_array_i = data.split(";");
    		String[] dataArray_counter;
    		String[] EndonetID; // 0
    		String[] Hauptname; // 1
    		String[] Hormonklasse; // 2
    		String[] Swissprot; // 3
    		String[] EnsembleID; // 4
    		int i = 0;
    		
    		int counter = 0;
    
    		while(data != null){
    			data = reader.readLine();
    			data_array_i = data.split(";");
    			System.out.println(java.util.Arrays.toString(data_array_i));
    		i++;
    		}
    
    		
    		
    		
    
    	} catch( FileNotFoundException filenotfoundexxption ) {
          System.out.println( "myFile.txt, does not exist" );
       } catch( IOException ioexception )	{
          ioexception.printStackTrace( );	
    		}
    	}
    }
    The error confuses me, because it does what I want it to before.
    It prints out the last line in the text-document, and then prints the error.
    I thought I might have an endless-loop or something, but I had (while data != null), so
    that's when the program should stop.
    What do I miss?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: FileReader and calling a method which gives back an array

    Your code tests the value of data BEFORE it changes data's value. You need to test data's value AFTER you change it.

Similar Threads

  1. ERROR: Calling array from a method
    By agente47 in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 03:18 PM
  2. Replies: 4
    Last Post: 02-21-2011, 03:52 AM
  3. [Java3D] Shape3D to byte array and back
    By Dennis in forum Advanced Java
    Replies: 0
    Last Post: 12-07-2010, 03:32 PM
  4. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 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
  •