0

I have a method that counts the occurrences of words in a text file, and returns the number of time the word is found on a particular line. However, it doesn't keep track of which line number the words are located. i have a separate method that counts the number of lines in the text file and i would like to combine the two methods into a method that tracks the line numbers, and keeps a log of the words occurrences on each line.

here are the two methods i would like to combine to give a result something like "Word occurs X times on line Y"

public class Hash
{
   private static final Object dummy = new Object();  // dummy variable
   public void hashbuild()
   {
      File file = new File("getty.txt");
      // LineNumberReader lnr1 = null;
      String line1;
      try{
         Scanner scanner = new Scanner(file);
         //lnr1 = new LineNumberReader(new FileReader("getty.txt"));

         // try{while((line1 = lnr1.readLine()) != null)
         //   {}}catch(Exception e){}

         while(scanner.hasNextLine())
         {
            String line= scanner.nextLine();
            List<String> wordList1 = Arrays.asList(line.split("\\s+"));   
            Map<Object, Integer> hm = new LinkedHashMap<Object, Integer>();
            for (Object item : wordList1)
            {
               Integer count = hm.get(item);
               if (hm.put(item, (count == null ? 1 : count + 1))!=null)
               {
                  System.out.println("Found Duplicate : " +item);
               }
            }
            for ( Object key : hm.keySet() )
            {
               int value = hm.get( key );
               if (value>1)
               {
                  System.out.println(key + " occurs  " + (value) + " times on line # "+lnr1.getLineNumber());
               }
            }
         }
     } catch (FileNotFoundException f)
     {f.printStackTrace();} 
   }
}

here is my original line counting method

public void countLines()
{
   LineNumberReader lnr = null; String line;
   try
   {
      lnr = new LineNumberReader(new FileReader("getty.txt"));
      while ((line = lnr.readLine()) != null)
      {
         System.out.print("\n" +lnr.getLineNumber() + " " +line);
      }
      System.out.println("\n");
   }catch(Exception e){}
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
user1660948
  • 59
  • 1
  • 5
  • Why aren't you using the commented-out code? It seems like that's meant to count lines for you. Does it have a problem or are you asking for a different approach? – Ted Hopp Nov 18 '12 at 19:15
  • the commented-out code doesnt give me what i am looking for really. it keeps track of the linenumber, but i would like to combine it with the other method into a All-in-One method. i commented it out because it wasnt working, but i left it in so you could see where i was trying to go – user1660948 Nov 18 '12 at 19:18
  • @Ankur so I have to accept all the questions i ask? regardless of them helping me? – user1660948 Nov 18 '12 at 19:32
  • 1
    @user1660948 no the only ones that help you, like the one I posted above in the comments – Ankur Nov 19 '12 at 08:59

1 Answers1

0

Why don't you just remember the line number in the while loop? Initialize a new variable and increase it when calling nextline.

sorencito
  • 2,517
  • 20
  • 21