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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:50:52+00:00 2026-06-01T02:50:52+00:00

My task is to write a class RFind which recursively searches the current directory

  • 0

My task is to write a class RFind which recursively searches the current directory and its subdirectories, attempting to match files against its single regular expression argument.

So far the only thing I have tried is the following which a) isn’t recursive and b) doesn’t really seem to work. How would you go about doing this?

NB: The sample solution given displays how you could pass regex in the command line and how it would look:

$ java RFind “.*.java”

./RFind.java

./src/AHTNF.java

$

public class RFind {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.print("Please enter a regular expression: ");
        String regex = in.next();

        Pattern p = Pattern.compile(regex);

        File dir = new File (".");

        File[] files = dir.listFiles();
        ArrayList<String> f = new ArrayList<String>();

        for (int i = 0; i < files.length; i++) {
            Matcher m = p.matcher(files[i].toString());
            if (m.matches()) {
                f.add(files[i].toString());
            }
        }

        for (int i = 0; i < f.size(); i++) {
            System.out.println(f.get(i));
        }

    }

}
  • 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-01T02:50:54+00:00Added an answer on June 1, 2026 at 2:50 am

    To be recursive you need at least one method to be called

    public class Rec {
    
        public static void main(String[] args) {
    
            Scanner in = new Scanner(System.in);
    
            System.out.print("Please enter a regular expression: ");
            String regex = in.next();
    
            Pattern p = Pattern.compile(regex);
    
            new Rec (p).start (".");
        }
    
        private Pattern p;
    
        Rec (Pattern pP)
        {
           p = pP;
        }
    
        void start (String pDir) {
            File dir = new File (pDir);
    
            File[] files = dir.listFiles();
            if (files == null) return;
            for (int i = 0; i < files.length; i++) {
                String x = files[i].toString();
                if (".".equals(x)) continue;
                if ("..".equals(x)) continue;
    
                Matcher m = p.matcher(x);
                if (m.matches()) {
                    System.out.println(x);
                }
                if (files[i].isDirectory ()) {
                   start (pDir+File.separator + x);
                }
            }
        }
    

    May run, not compiled yet

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

Sidebar

Related Questions

My task is to write an app(unfortunatly on C) which reads expression in infix
How to write an ant task that removes files from a previously compiled JAR?
I want to write a simple task which will update and commit source code
I have a task to write a set of http unit tests, which should
I am about to write a task which will be run once in about
I had a task to write simple game simulating two players picking up 1-3
I had a task to write simple game simulating two players picking up 1-3
I was given a task of write the coding guidelines for my team, and
I have been assigned wit the task to write a program that takes a
In my task would be very nice to write a kind of objects serialization

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.