Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: word counter program

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool word counter program

    INSTRUCTIONS
    1. Create a WordCounter class with a constructor that takes a file name as a parameter
    2. The class should have two fields: one for the file name and one for a HashMap to store word count information
    3. The constructor should call a private method, countWords, that reads in the file and counts the word frequencies
    4. The class should contain a get method for each field, as well as a print method that prints out the map in the following format: word: frequency
    5. When printing, the map should be sorted by either the word order or frequency (Hint: see Collections.sort)
    6. You should include the sample text file on Blackboard



    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Scanner;
     
     
     
    public class WordCounter 
    {
    	// 2 fields for file name and hashmap
    	private ArrayList<String> fn,hm;
    	private Scanner x;
     
    	// constructor
    	public WordCounter(String frame)
    	{
    		countwords();
     
    	}
    	// methods
    	private void countwords ()
    	{
    		try{
    			x = new Scanner (new File("test.file.txt"));
    		System.out.println(Collections.frequency(fn, hm));
    		}
    		catch (Exception e){
    			System.out.println("File not found");
    		}
    	}
    	private void print() {
     
     
    	}
    	public static void main(String[] args) 
    	{
    		String fname = "test-file.txt";
    		if (args.length > 0)
    			fname = args[0];
     
    		WordCounter wc = new WordCounter(fname);
    		wc.print();
    	}
     
     
    }

    I still need help with step 4 and 5. please help im still learning
    Last edited by shad0wblade890; March 4th, 2014 at 12:04 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: word counter program

    The steps are not numbered. Which are 4 & 5?

    Please be more specific describing your problems.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: word counter program

    done and i need help on the print method as you can see i left it blank because idk where to start. also i want to make sure that the code i did matches the instructions

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: word counter program

    need help on the print method
    What is the printed output supposed to look like? Where does the data that is to be printed come from?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. need help troubleshooting a word counter program
    By EatSleepProgram in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2013, 04:41 PM
  2. Word Scramble Program Help
    By beginneratjava in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 18th, 2012, 12:24 PM
  3. pls help me with word occurances program
    By kashif in forum Collections and Generics
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  4. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  5. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM