Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NEED HELP.. with a simple word count program!

    I'm having trouble with a word count program. I need to count the number of occurrences of various words in a paragraph and increment their count. I just cannot figure out how to add words to the Array in this case the Array WC. THANKS!
    Heres what I have:

    //*********************************************************************************
    // WordCounter.java Author: JTH
    //*********************************************************************************
    import java.util.Scanner;
    import java.io.*;
    public class WordCounterV1 {

    /////////////////////////////////////////////////////////////////////////////
    // A local method useful for aligning the ouput; it returns the given
    // string with an appropriate number of blanks appended so that the
    // resultant length equals the given length.
    /////////////////////////////////////////////////////////////////////////////
    static String padded(String S, int Width) {
    String result = S;
    while(result.length() < Width) {
    result = result + " ";
    }
    return result;
    }

    public static void main (String[] args) throws FileNotFoundException {
    ///////////////////////////////////////////////////////////////////////////
    // The following declares an array of counters; one for each of up to LIMIT words
    final int LIMIT = 256;
    WordCount[] WC = new WordCount[LIMIT]; //The array of WordCount objects
    int next = 0; //The number of elements used in the array
    final String DEFAULT = "*UNCOUNTED*"; //Used to indicate "uncounted words"
    ///////////////////////////////////////////////////////////////////////////

    System.out.println("JTH's WordCounterV1...");
    if(args.length == 1) {
    Scanner Stream = new Scanner (new File(args[0]));
    String S;

    final String BLANK = " ";
    int location;
    while (Stream.hasNext()) {
    S = Stream.nextLine().trim();


    // THIS IS WHERE I NEED HELP ADDING WORDS TO THE ARRAY LIST !!!!!!!!
    }

    // Loop to determine the length of the longest word
    int max = 0;
    for(int i=0; i<next; i++) {
    if(WC[i].Word().length() > max) {
    max = WC[i].Word().length();
    }
    }

    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    // Loop to now print out each word with its corresponding count
    for(int i=0; i<next; i++) {
    System.out.println(padded(WC[i].Word(),max) + " appeared " + WC[i].Count() + " times");
    }
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }
    else {
    System.out.println("===ERROR: Required filename argument missing");
    }
    }
    }

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    like this

    Code:
    WC[0] = new WordCount(...);
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks...I just started programming and my professor expects us to know more than we really do.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •