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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:46:16+00:00 2026-06-01T17:46:16+00:00

This is my first post here so kindly pardon any mistakes that I have.

  • 0

This is my first post here so kindly pardon any mistakes that I have. I’m still learning to find my way around Stack Exchange.

I am trying to write a Java program that tries to scan a Directory full of either .txt,.rtf or.doc files(and none other). The aim is to search all the files in the directory, and find out if a particular string exists in the file. If it does, it returns the string and the filename that it found the string in.

The aim of this program is, it is a project for school wherein the program scans the personal folders of call center employees to check if they have stored any CC/DC nos and if yes, report the folder name – to reduce CC fraud.

The search function was fairly straight forward and works when I individually specify the filename. However, the searching the directory and passing the files to the search function has me stumped.

I’ve posted my code so far, if you guys could look thru it and give me some feedback/suggestions, I’d really appreciate it. Thanks in advance

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

public class parse2{

void traverse(String directory) throws FileNotFoundException   
    {  
        File dir = new File(directory);  
            if (dir.isDirectory())  
        {  
                   String[] children = dir.list();  
                   for (int i=0; i<children.length; i++)   
                {  
            //System.out.println("\n" + children[i]);
                    reader(children[i]);  
                }  
            }  

     }


void reader(String loc) throws FileNotFoundException
{   //System.out.println("\nC:/Documents and Settings/h4d35/Desktop/javatest/chk/"+loc);
    String s = ("C:/Documents and Settings/h4d35/Desktop/javatest/chk/"+loc);
    //System.out.println("\n"+s);
            FileReader fr = new FileReader(loc);
    BufferedReader br = new BufferedReader(fr);
    Scanner sc = new Scanner(br);
    char[] chkArray;
    int chk=1;
    char ch;
    while(sc.hasNext())
    {
        String chkStr = sc.next();
        chkArray = chkStr.toCharArray();
        if ((chkArray[0]=='4')&&(chkStr.length()>13))
        {   for(int i=0;i<chkArray.length;i++)
            {   ch=chkArray[i];
                if((ch=='0')||(ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9'))
                {   chk=0;
                    continue;
                }
                else 
                {   chk=1;
                    break;
                }
            }
            if(chk==0)
                System.out.println("\n"+ chkStr);
        }
        else
        if((chkArray[0]=='5')&&(chkStr.length()>13))
        {   for(int i=0;i<chkArray.length;i++)
            {   ch=chkArray[i];
                if((ch=='0')||(ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9'))
                {   chk=0;
                    continue;
                }
                else 
                {   chk=1;
                    break;
                }
            }
            if(chk==0)
            System.out.println("\n"+ chkStr);
        }
        else
        if((chkArray[0]=='6')&&(chkStr.length()>13))
        {   for(int i=0;i<chkArray.length;i++)
            {   ch=chkArray[i];
                if((ch=='0')||(ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9'))
                {   chk=0;
                    continue;
                }
                else 
                {   chk=1;
                    break;
                }
            }
            if(chk==0)
            System.out.println("\n"+ chkStr);
        }
    }
}

public static void main(String args[]) throws FileNotFoundException
{   
    parse2 P = new parse2();
    P.traverse("C:/Documents and Settings/h4d35/Desktop/javatest/chk");
}

}

**

  • EDIT : The variable “loc” only gives the file name – so I added the rest of the path to it under the String variable “s”. There’s no output. When I uncomment Line 3 of reader() function, it shows the absolute path of all the files. I tried the reader() function on its own by explicitly specifying the absolute path and it worked. Code below:

    import java.io.*;

    import java.util.*;

public class parse1{

void read() throws FileNotFoundException
{   FileReader fr = new FileReader("C:/Documents and Settings/h4d35/Desktop/javatest/chk/Call back customer.txt");
    BufferedReader br = new BufferedReader(fr);
    Scanner sc = new Scanner(br);
    char[] chkArray;
    int chk=1;
    char ch;
    while(sc.hasNext())
    {
        String chkStr = sc.next();
        chkArray = chkStr.toCharArray();
        if ((chkArray[0]=='4')&&(chkStr.length()>13))
        {   for(int i=0;i<chkArray.length;i++)
            {   ch=chkArray[i];
                if((ch=='0')||(ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9'))
                {   chk=0;
                    continue;
                }
                else 
                {   chk=1;
                    break;
                }
            }
            if(chk==0)
                System.out.println("\n"+ chkStr);
        }
        else
        if((chkArray[0]=='5')&&(chkStr.length()>13))
        {   for(int i=0;i<chkArray.length;i++)
            {   ch=chkArray[i];
                if((ch=='0')||(ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9'))
                {   chk=0;
                    continue;
                }
                else 
                {   chk=1;
                    break;
                }
            }
            if(chk==0)
            System.out.println("\n"+ chkStr);
        }
        else
        if((chkArray[0]=='6')&&(chkStr.length()>13))
        {   for(int i=0;i<chkArray.length;i++)
            {   ch=chkArray[i];
                if((ch=='0')||(ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9'))
                {   chk=0;
                    continue;
                }
                else 
                {   chk=1;
                    break;
                }
            }
            if(chk==0)
            System.out.println("\n"+ chkStr);
        }
    }
}

public static void main(String args[]) throws FileNotFoundException
{   
    parse1 P = new parse1();
    P.read();
}

}

**

  • 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-01T17:46:18+00:00Added an answer on June 1, 2026 at 5:46 pm

    The biggest problem I see is that you should instead be recursively calling traverse() on directories, and should only call reader() on files. You should also try stepping through the problematic lines of code in a debugger. It’s really easy to set breakpoints and step through your code in Eclipse or probably any other IDE.

    You can easily filter by filename extension using the built-in API. Check out FilenameFilter and File.list(FilenameFilter) or (preferably) File.listFiles(FilenameFilter). Whenever possible, you should try to avoid storing file and directory paths as Strings–instead, use File objects.

    Your reader method could be simplified by using String.matches(...) or String.regionMatches(...). Both of these methods (and similar ones) are regular expression comparisons–take a look at the Pattern class, in particular. Since you’re repeating the same comparisons a lot, you may want to create one or more Patterns to reuse over and over, but that’s just a performance optimization.

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

Sidebar

Related Questions

This is my first post here. I have a problem. I need to take
Well, this is my first post here and really enjoying the site. I have
This is my first post here, therefore apologize for any blunders. I'm developing a
Hello all this is my first post here i have been working in an
This is my first post here and I hope that someone will be able
This is my first post here, recently i have been working with JSF2.0 with
First post here, I have tried searching but couldn't find what I'm looking for
This is my first post here, and i should advise that my knowledge is
This is my first post here. I have been reading posts here since I
This is my first post here and I wanted to get some input from

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.