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

  • Home
  • SEARCH
  • 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 8991691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:47:14+00:00 2026-06-15T22:47:14+00:00

The program that I am trying to create is a program that takes words

  • 0

The program that I am trying to create is a program that takes words from a user defined file, saves those words as variables and then searches a different user defined file for those words, outputting there location.

The program works up to and including the point where the program takes the words and saves them as variables. The problem with the program is that the search method returns a null result. My main suspicions are that the code in the search method is incompatible with the code in the read method, or that the 2 methods aren’t running simultaneously.

The search method is in the searching class and the read method is in the reading class.

Here is my code (Containing all 3 of my classes), please excuse all of the imports.

This is the first class:

import java.io.FileNotFoundException;
import java.util.Scanner;

public class Combination{

    public static void main(String[] args) throws FileNotFoundException{

    Scanner userInput = new Scanner(System.in);
    Reading ReadingObject = new Reading();        
    System.out.println("Please enter the file that you wish to open");
    String temp = userInput.nextLine();
    ReadingObject.setFileName(temp);
    ReadingObject.read();
    Scanner searchForWord = new Scanner(System.in);
    Searching SearchingObject = new Searching();
    System.out.println("Please enter the file that you would like to search for these words in");
    String temp1 = searchForWord.nextLine();
    SearchingObject.setFileName(temp1);
    SearchingObject.search();

}    
}

This is the second class:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

class Reading {
private String file;
public void setFileName(String fileName){
    file = fileName;
}
public String getFileName(){
    return file;
}
public void read(){
    try{
        //Choosing the file to open
        FileInputStream fstream = new FileInputStream(getFileName());

        //Get the object of datainputstream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine = null;

        //Read the file line by line
        while((strLine = br.readLine()) != null){
            //      \\s+ means any number of whitespaces between tokens
            String [] tokens = strLine.split("\\s+");
            String [] words = tokens;
            for(String word : words){
                System.out.print(word);
                System.out.print(" ");

                Searching SearchingObject = new Searching();
                SearchingObject.setWord(word);
            }
            System.out.print("\n");   
        }
        in.close();  
    }
    catch(Exception e){
        System.err.println("Error: " + e.getMessage());  
    }
}
}

This is the third class:

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Searching {
private String file1;
public void setFileName(String fileName){
    file1 = fileName;
}
public String getFileName(){
    return file1;
}
private String word1;
public void setWord(String wordName){
    word1 = wordName;    
}
public String getWord(){
    return word1;
}

public void search() throws FileNotFoundException{

    try{
        //Choosing the file to open
        FileInputStream fstream = new FileInputStream(getFileName());

        //Get the object of datainputstream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine = null;

        while((strLine = br.readLine()) != null){

            Pattern p = Pattern.compile(getWord());
            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();
                }
          }        
    }
    catch(Exception e){
        System.err.println("Error: " + e.getMessage());
    } 
}
}

Any help will be greatly 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-15T22:47:15+00:00Added an answer on June 15, 2026 at 10:47 pm

    Your code is hard to read. Your reading class does not only read; it also searches. You should call it something that reflects its intended use. However, it forgets to tell its searching object where to search, and does not pass the reference to this object to anyone else. In this snippet

    for (String word : words) {
        System.out.print(word);
        System.out.print(" ");
    
        searching searchingObject = new searching();
        searchingObject.setWord(word);
    }
    

    you are essentially not doing anything. The reference to searchingObject is lost forever.

    Your reading class should keep an ArrayList of words to be searched for in the searching, instead of instancing searching objects.

    Your searching class should take, as a constructor parameter, one of these ArrayLists — and convert it into a single regex, which is much more efficient than reading the file once per word to search for. You can search for “a”, “b” and “c” using the single regular expression “a|b|c”. Works with longer words, too. Escape them first to avoid problems.

    Oh, and please, please follow naming guidelines. Call your reading a TokenReader, and your searching a WordListSearcher…

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

Sidebar

Related Questions

I'm trying to create a program that takes a text file of c++ code
Hello i'm trying to write a program that takes 3 user inputs: file path,
I am trying to create a program that will get data directly from socket
I am trying to create a program that will be passed input data from
I am trying to make a small program that takes in input from a
I am trying to create a program that will open a text file with
I was trying to create a program that takes arguments by command line, using
Hi i'm trying to create a sever/client program that takes up to 5 clients
I'm trying to create a simple temperature conversion program that allows the user to
I am trying to create a program that will take user inputs(marks) and output

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.