Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:49:34+00:00 2026-05-27T12:49:34+00:00

first off thank you for the pointer that something.endsWith doesn’t do what i was

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:49:34+00:00Added an answer on May 27, 2026 at 12:49 pm

    Personally I never tried to call method of my class with keyword class. If we say that your class is named Foo, in your Main method you should write:

    mzList = Foo.filreader("precursor",mzList);
    

    In your first Main example, this doesn’t make sense:

    List mzList = new LinkedList();
    mzList = new ArrayList();
    

    It should be either LinkedList() or ArrayList(), like so:

    List mzList = new LinkedList(); or List 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:

    public static void 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);
    }
    

    and then simply (notice there is no assignment):

    Foo.filreader("precursor",mzList);
    

    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:

    public static LinkedList filereader (String filename) {
       LinkedList someList = new LinkedList(); // notice list initialization
       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);
    }
    

    and then simply call your method and assign your results:

    mzList = Foo.filreader("precursor");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm working on an app that's written such that some of your
First off, let me start off that I am not a .net developer. The
First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS
First off, thank you to everyone on this site...it's been INCREDIBLY helpful in getting
First off, I know there are ways to make it so that text can
First off, thank you to everyone for your help!!! All I'm trying to do
First off thank you in advance for taking time to help me with this,
First off, I'd like to apologize for the horrid title there, but it was
First off, I'm sorry if the title doesn't explain this very well. I'm looking
First off, I am using Windows XP. I have multiple hard drives and it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.