first off thank you for the pointer that something.endsWith doesn’t do what i was expecting it to do yesterday. Now the reason i would like to borrow your brains today is the following; i have written my program and it does exactly what i need but it’s all in a chunky ‘main’ so i wanted to shift some parts of the code to methods to make it more readable/mantainable and i am struggling with a method that works with a LinkedList.
The code as part of the main program was as follows:
/* Variable definition */
List mzList = new LinkedList();
mzList = new ArrayList();
/* Reading the input file */
try {
FileInputStream a_stream = new FileInputStream("./precursors");
DataInputStream a_in = new DataInputStream(a_stream);
BufferedReader a_reader = new BufferedReader(new InputStreamReader(a_in));
String a_line;
while ((a_line = a_reader.readLine()) != null) {
if (a_line.equals("---")) {
/* Do Nothing... */
} else {
mzList.add(a_line);
}
} /* ends the loop that reads input file */
a_in.close(); */
}
I have been trying to write this into a method as follows:
public static LinkedList filereader (String filename, LinkedList someList) {
FileInputStream input = new FileInputStream(filename);
DataInputStream in = new DataInputStream(input);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
if (line.equals("---")) {
/* Do Nothing... */
} else {
someList.add(line);
}
}
in.close();
return(someList);
}
I would then attempt to call this in the main program as follows:
mzList = class.filreader("precursor",mzList);
which gives me a bunch of problems, obviously i am doing something stupid and i beg forgiveness for my harassing you all with this problem but i am quite new to Java and searching through the other ‘questions’ didn’t really help me solve my problem.
Kind Regards,
Bas Jansen
PS: Edited some blatant typo’s and formatting errors
Personally I never tried to call method of my class with keyword
class. If we say that your class is namedFoo, in your Main method you should write:In your first Main example, this doesn’t make sense:
It should be either LinkedList() or ArrayList(), like so:
List mzList = new LinkedList();orList mzList = new ArrayList();Next, you are passing file name “precursor”, but in the first example you are using “./precursors” (but I think you are aware of that).
Lastly you are assigning result of your method to variable
mzList, and pass the same list instance as parameter. If you pass list as parameter, you don’t have to return it back because it the very same list as created earlier in your code (read about references). You could use something like this:and then simply (notice there is no assignment):
As far I see, you are not making use of this input list in terms of reading data from it. Thus, you can initialize your list inside the method and return it, like so:
and then simply call your method and assign your results: