Results 1 to 11 of 11
- 12-03-2011, 12:36 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
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:
and b/c I'm really really sure, that I'm not nearly done, I wrote a class to test this one: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; } }
but if I try to run the test class, nothing happens.Java Code:class TestPeph{ public static void main(String[] args){ PepHormon tryit = new PepHormon(); System.out.println(tryit.einlesen()); } }
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
- 12-03-2011, 01:07 PM #2
Member
- Join Date
- Dec 2011
- Posts
- 19
- Rep Power
- 0
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
- 12-03-2011, 01:35 PM #3
Re: FileReader and calling a method which gives back an array
Add some println statements to your code to show the execution flow and the values of variables as the code executes.if I try to run the test class, nothing happens.
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.
- 12-03-2011, 01:45 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
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 ;)
- 12-03-2011, 01:50 PM #5
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.
- 12-03-2011, 01:54 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 19
- Rep Power
- 0
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. =)
- 12-03-2011, 02:29 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
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":
this is my code:Java Code:PepHormon.java:31: cannot find symbol symbol : variable data location: class PepHormon System.println(data[1]); ^ 1 error
I tried to print out just one position in the array, just to see if it works, but it doesn't.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]); } }
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:
but now it saysJava 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]); } } }I don't understand whats going on. Why can it always not find some symbol?!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
Last edited by cups; 12-03-2011 at 02:43 PM.
- 12-03-2011, 02:40 PM #8
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.
- 12-03-2011, 02:46 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
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.
- 12-04-2011, 10:17 AM #10
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
Re: FileReader and calling a method which gives back an array
Hey!
I don't know what happened, but I'm getting a
-error!Java Code:Exception in thread "main" java.lang.NullPointerException at PepHormon.einlesen(PepHormon.java:23) at TestPeph.main(TestPeph.java:7)
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.
The error confuses me, because it does what I want it to before.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( ); } } }
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?
- 12-04-2011, 01:16 PM #11
Similar Threads
-
ERROR: Calling array from a method
By agente47 in forum New To JavaReplies: 2Last Post: 03-25-2011, 03:18 PM -
how to loop through an array and jump back to beginning
By drhinri in forum New To JavaReplies: 4Last Post: 02-21-2011, 03:52 AM -
[Java3D] Shape3D to byte array and back
By Dennis in forum Advanced JavaReplies: 0Last Post: 12-07-2010, 03:32 PM -
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks