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 9023981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:53:42+00:00 2026-06-16T05:53:42+00:00

I am trying to make a program that takes input from a file and

  • 0

I am trying to make a program that takes input from a file and then searches for the input in a different file, for example, it takes the word “car” from file A and outputs the location of the word(if the word is included) in file B.

At the moment I have 3 classes. A main class, a class to read the file input and a class to search for the input in another file.

The class that reads the input has code that takes the files, reads the file line by line and saves each word into a variable, and then adds these variables into an arrayList.

This is part of the code:

List<String> listOfWords = new ArrayList<String>();
     while((strLine = br.readLine()) != null){
          String [] tokens = strLine.split("\\s+");
          String [] words = tokens;
          for(String word : words){
              listOfWords.add(word);
              System.out.print(word);
              System.out.print(" ");      
          } 
        System.out.print("\n");  
    }
        in.close();
        return listOfWords;    
    }

I then need to take the arrayList and put it into the class with the code that searches for the words in an external file.

This is part of the code that searches for the words:

public void search(List<String> listOfWords) throws FileNotFoundException, IOException{

        FileInputStream fstream = new FileInputStream(getSearchFileName());

        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

        while((strLine = br.readLine()) != null){
            for(String list: listOfWords){
            Pattern p = Pattern.compile(list);
            Matcher m = p.matcher(strLine);

        int start = 0;
        while (m.find(start)) {
            System.out.printf("Word found: %s at index %d to %d.%n", m.group(), m.start(), m.end());
            start = m.end();
                }
            }
          }  
}

The code works standalone, but I am having trouble calling the arrayList (listOfWords) from the first set of code and using it in the second set of code, which are in different classes.

Any help with what code I need to add or adjust will be appreciated.

Regards

  • 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-06-16T05:53:43+00:00Added an answer on June 16, 2026 at 5:53 am

    This seems like a matter of scope. If you’re going to use that list across a class or between methods you can consider:

    1. Returning the list on the first method, saving it in a field or variable and passing it to the second method.
    2. Make the list a class field and manage it. That way you’ll be able to access it in the second block of code, assuming both blocks are on the same class.

    As it is, the list is defined within the scope of the first block of code (the while loop), which I presume is an extract of an actual method. Once the method is finished, the list is eligible for GC (in other words, as good as discarded).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a small program that takes in input from a
I've been trying to make a simple program that takes the user input, until
A am trying to make a program that takes a signal from one class
I am trying to make a program that can take an .mp3 file from
I'm trying to make a program that takes in one argument, a file, and
I'm trying to make a program that lets you input any one-step equation, and
I am trying to make a program that will find similar images from a
I'm trying to make a program which Takes the user input (let's say all
I'm trying to make a class that takes info from the GUI this saves
I am trying to make a program that will take as input a string

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.