0

I am trying to count the number of occurrences of days of the week in a textfile. As of right now my code counts the total number of occurrences but I need it to count the number of individual occurrences of each keyword. The output needs to look like this

Monday = 1
Tuesday = 2
Wednesday = 0

etc.

Here's my code so far

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

public class DayCounter
{

public static void main(String args[]) throws IOException 
{

    String[] theKeywords = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    // put each keyword in the map with value 0 
    Map<String, Integer> DayCount = new HashMap<String, Integer>();
    for (String str : theKeywords)
    {
        DayCount.put(str, 0);
    }

    try (BufferedReader br = new BufferedReader(new FileReader("C:\\Eclipse\\test.txt")))
    {

        String sCurrentLine;

        // read lines until reaching the end of the file
        while ((sCurrentLine = br.readLine()) != null) 
        {


            if (sCurrentLine.length() != 0) 
            {

                // extract the words from the current line in the file
                if (DayCount.containsKey(sCurrentLine))
                {
                    DayCount.put(sCurrentLine, DayCount.get(sCurrentLine) + 1);
                }
            }
        }

    } 
    catch (FileNotFoundException exception)
    {

        exception.printStackTrace();
    } 
    catch (IOException exception) 
    {

        exception.printStackTrace();
    } 


    int count = 0;
    for (Integer i : DayCount.values()) 
    {
        count += i;
    }

    System.out.println("\n\nCount = " + count);
}
}
6
  • 4
    Whats not working at this point? Commented Mar 5, 2013 at 19:21
  • Its working but I can't figure out how to change the output to what I described in the question Commented Mar 5, 2013 at 19:22
  • Your code seems fine to me. Commented Mar 5, 2013 at 19:22
  • Looks good to me. What exactly is your current output? Commented Mar 5, 2013 at 19:22
  • 1
    what is the output you get? What is your problem? Is there any exception? How the data in the text file are presented? Currently you are reading a line, not a word, in a line there might be number of words Commented Mar 5, 2013 at 19:26

4 Answers 4

1

Try this instead of just printing the sum :

for(String day : theKeywords) {
 System.out.println(day + " = " + DayCount.get(day));
}
Sign up to request clarification or add additional context in comments.

6 Comments

Actually if I put the same string on a line it only counts it once(ex. Monday Monday)
@user2007843 then you need to split the strings, trim them and load into HashMap
sorry can you explain that a little more in depth?
@user2007843 So instead of checking for if (DayCount.containsKey(sCurrentLine)), use split on sCurrentLine which will give you String[]. Just iterate this array and check if any of these elements exist in DayCount`
is there a way to do this using indexof?
|
1

You are printing the sum of all the days. Instead you want to print the value for each day. So instead of

  for (Integer i : DayCount.values()) 
    {
        count += i;
    }

You should do

for(String Day: theKeywords) {
 System.out.println(Day+ " = " + DayCount.get(day));
}

Comments

0

Try this:

for (String crtDay : dayCount.keySet())
    System.out.println(String.format("%s = %d", crtDay, dayCount.get(crtDay));

Comments

0

Simply do this:

for (Entry<String, Integer> count: DayCount.entrySet())
    System.out.println(count.getKey()+" = "+count.getValue());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.