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

    Help with beginner problem...involves Arrays

    Hi I'm having trouble with my word count program. The program reads input from a .txt file and then inputs each word into an array (WC array). It is then supposed to increment the count of each word and if a new word is found then add it to an array and increment its count for each occurrence. I'm stuck on the IF-else statement and also on how to search an existing array to see if the word is there and if not then add that to the array. Heres what I have so far. I'm stuck with the if else statement father down. The program compiles but when run it shows no print out.

    Code:
    //*********************************************************************************
    // WordCounter.java Author: JTH
    //*********************************************************************************
    import java.util.Scanner;
    import java.io.*;
    public class WordCounterV2 {
    
    /////////////////////////////////////////////////////////////////////////////
    // 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();
    
    //I AM STUCK HERE UNTIL AFTER THE ELSE STATEMENT
    // S.split(BLANK);
    for (int i= 0; i < next; i++) {
    if (S.equals(WC[i].Word())){
    WC[i].Increment(); // if word found, increment count of that word.
    
    }
    
    
    else{
    WC[next] = new WordCount(S);// add word to length
    next = next + 1;
    WC[i].Increment();
    }
    }
    
    
    }
    // 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
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    anyone

    anyone please?

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    you're mostly on the right track...but, some logic issues are present...for e.g. you are not checking all the elements of your WC array before adding a new word to it...that should help you resolve your problem
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

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