Run-time error when trying to count the list of the number of words in a file

advertisements

What I'm trying to do is make my program list the words in the text file and how many times a certain word prints out.

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

I've done it so it prints the number of words but not the frequency of each word, I've created a hash map but I'm getting a run time error on line 38. Any help would be appreciative.

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();
 }
}

Here's my other class

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
      {
         Scanner in = new Scanner(new File(filename));

         while (in.hasNext())
         {
            in.next();
            count++;
         }
         System.out.println(filename + " : " + count);
      }
      catch (FileNotFoundException e)
      {
         System.out.println(filename + " was not found.");
      }

      HashMap<String, Integer> map = new HashMap<String, Integer>();
      {

      Scanner in = null;
    while (in.hasNext()) {
    String word = in.next();
          if (map.containsKey(word))
              map.put(word, map.get(word) + 1);
          else{
                  map.put(word, 1);
      }

      }

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

   }

   }

}


You have to merge both loops:

      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);

because if you use two loops, the first will read the whole file and the pointer is located at the end of the file, so in the second loop there are no data to read and in.hasNext() returns false.

UPDATE The whole Code must look like:

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.");
        }
    }

}