0

I've created a program that will look at a text file in a certain directory and then proceed to list the words in that file.

So for example if my text file contained this.

hello my name is john hello my

The output would show

hello 2

my 2

name 1

is 1

john 1

However now I want my program to search through multiple text files in directory and list all the words that occur in all the text files.

Here is my program that will list the words in a single file.

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

public class WordCountstackquestion implements Runnable {

    private String filename;

    public WordCountstackquestion(String filename) {
        this.filename = filename;
    }

    public void run() {
        int count = 0;
        try {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            Scanner in = new Scanner(new File(filename));

            while (in.hasNext()) {
                String word = in.next();

                if (map.containsKey(word))
                    map.put(word, map.get(word) + 1);
                else {
                    map.put(word, 1);
                }
                count++;

            }
            System.out.println(filename + " : " + count);

            for (String word : map.keySet()) {
                System.out.println(word + " " + map.get(word));

            }
        } catch (FileNotFoundException e) {
            System.out.println(filename + " was not found.");
        }
    }

}

My main class.

 public class Mainstackquestion
    {

       public static void main(String args[])
       {
         if(args.length > 0)
         {
           for (String filename : args)
           {

             CheckFile(filename);
           }
         }
         else
         {

           CheckFile("C:\\Users\\User\\Desktop\\files\\1.txt"); 
         }

       }

 private static void CheckFile(String file)
 {
     Runnable tester = new WordCountstackquestion(file);
     Thread t = new Thread(tester);
     t.start();
 }
}

I've made an attempt using some online sources to make a method that will look at multiple files. However I'm struggling and can't seem to implement it correctly in my program.

I would have a worker class for each file.

 int count;

   @Override
   public void run()
   {
      count = 0;
      /* Count the words... */
      ...
      ++count;
      ...
   }

Then this method to use them.

public static void main(String args[]) throws InterruptedException
   {
      WordCount[] counters = new WordCount[args.length];
      for (int idx = 0; idx < args.length; ++idx) {
         counters[idx] = new WordCount(args[idx]);
         counters[idx].start();
      }
      int total = 0;
      for (WordCount counter : counters) {
        counter.join();
        total += counter.count;
      }
      System.out.println("Total: " + total);
   }

1 Answers1

0

I'm going to assume that all of these files lie in the same directory. You can do it this way:

public void run() {
    // Replace the link to your filename variable
    File f = new File("link/to/folder/here");
    // Check if file is a directory (always do this if you are going to use listFiles()
    if (f.isDirectory()) {
        // I've moved to scanner object outside the code in order to prevent mass creation of an object
        Scanner in = null;
        // Lists all files in a directory
        // You could also use a for loop, but I prefer enchanced for loops
        for (File file : f.listFiles()) {
            // Everything here is your old code, utilizing a new file (now named "f" instead of "filename"
            int count = 0;
            try {
                HashMap<String, Integer> map = new HashMap<String, Integer>();
                in = new Scanner(f);

                while (in.hasNext()) {
                    String word = in.next();

                    if (map.containsKey(word))
                        map.put(word, map.get(word) + 1);
                    else {
                        map.put(word, 1);
                    }
                    count++;

                }
                System.out.println(f + " : " + count);

                for (String word : map.keySet()) {
                    System.out.println(word + " " + map.get(word));

                }
            } catch (FileNotFoundException e) {
                System.out.println(file + " was not found.");
            }
        }
        // Once done with the scanner, close it (I didn't see it in your code, so including it now)
        in.close();
    }
}

If you wanted to use a for loop rather than an enhanced for loop (for compatibility purposes), the link shared in the comments.

Otherwise, you can just keep scanning user input, and throwing it all into an ArrayList (or some other form of an ArrayList, whatever is required for your needs) and loop through the arraylist and move around the "File f" variable (to inside the loop), sorta like this:

for(String s : arraylist){
    File f = new File(s);
}
Community
  • 1
  • 1
Stephen
  • 376
  • 1
  • 3
  • 13
  • Thanks for the help, however when I tired to implement your code my program simply terminates and nothing is outputted? All I've done is changed my run method in my class WordCountstackquestion to yours and then ran my Mainstackquestion class. Any reason why nothing is outputted and the program just terminates? Thanks for the help in advance! – LookingWest Apr 27 '17 at 22:48
  • I looked over it, and I called upon the "f" variable rather than the "file" variable in method. Goes to show you shouldn't copy and paste code, but rather, understand how it works and implement it yourself (as in like recoding). – Stephen Apr 27 '17 at 22:59