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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:53:09+00:00 2026-05-13T01:53:09+00:00

I have a small java program that searches the contents of all *.txt files

  • 0

I have a small java program that searches the contents of all *.txt files in a folder for a specific string.

Example of my problem:

  1. Enter string to find:
  2. Enter string to find: 6570
  3. Enter string to find: 6570
    Found 2 time(s) in kyle.txt!
    Found 2 time(s) in kylezz.txt!
    Press any key to continue . . .

The problem:

It is searching for 6570 however I pickup results for values like this with that string:

11111116570111111
657011111
111116570
6570

Question: I want to search for only an exact string, eg:”6570″. how can I make it return only the exact value of 6570? I do not want any extra characters at the beginning or end, only the exact value.

Here is my code:

import java.io.*;
import java.util.*;

public class FileScanner {

    public static void main(String args[]) {

        System.out.print("Enter string to find: ");
        Scanner sc = new Scanner(System.in);
        find(sc.nextLine());

    }

    public static void find(String delim) {
        File dir = new File("files");
        if (dir.exists()) {
            String read;
            try {
                File files[] = dir.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File loaded = files[i];
                    if (loaded.getName().endsWith(".txt")) {
                        BufferedReader in = new BufferedReader(new FileReader(
                                loaded));
                        StringBuffer load = new StringBuffer();
                        while ((read = in.readLine()) != null) {
                            load.append(read + "\n");
                        }
                        String delimiter[] = new String(load).split(delim);
                        if (delimiter.length > 1) {
                            System.out.println("Found "
                                    + (delimiter.length - 1) + " time(s) in "
                                    + loaded.getName() + "!");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("error: dir wasn't found!");
        }
    }
}

Thanks guys I really hope you can help me with my programming problem. I’ve been trying to fix it for about a month now and now I’m asking for help.

  • 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-13T01:53:09+00:00Added an answer on May 13, 2026 at 1:53 am

    Would something like this work?

      public static void find(String delim) {
        File dir = new File("/tmp/files");
        Pattern strPattern = Pattern.compile(delim);
        if (dir.exists()) {
            try {
                for(File curFile : dir.listFiles()){
                  if(!curFile.getName().endsWith(".txt")){continue;}
                  BufferedReader in = new BufferedReader(new FileReader(
                           curFile));
                  int foundCount = 0;
                  String read = null;
                  while ((read = in.readLine()) != null) {
                    if(strPattern.matcher(read).matches()){
                      foundCount ++;
                    }
                  }
                  System.out.println("Found "+ delim +" "+ foundCount + " time(s) in "+curFile);
    
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("error: dir wasn't found!");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a small Java program which I would like to run both
As a Christmas gift I have written a small program in Java to calculate
I'm having a small problem in Java. I have an interface called Modifiable. Objects
I have written a small java application for which I need to obtain performance
I have small problem with my .net 2.0 winforms application. I want to embed
I have a small JS function that does Ajax for me and another like
I have a small utility that I use to download an MP3 file from
I have small page which has label, DropDownList and a submit button. <div> <asp:label
Where I work, we have small teams of 2 - 5 people. As a
I have a small local network. Only one of the machines is available to

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.