Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,284 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 995 people online right now. Registration is fast and FREE... Join Now!




Help With Word Count Program using Arrays

 
Reply to this topicStart new topic

Help With Word Count Program using Arrays, I'm stuck on a few parts of my program...

Jogan101
16 Nov, 2008 - 03:39 PM
Post #1

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 23

Ok so I need help 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 adding the words to the array 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 both my Word Count class and program...


WordCount Class
CODE
// ********************************************************************************
*
//  WordCount.java      Author: PMJ
// ********************************************************************************
*

    public class WordCount {
      private String word;                //The word represented as a string
      private int count = 0;            //The corresponding count
          
       public WordCount(String S) {    //The essential constructor
         word = S;
      }
          
       public String Word() {            //Accessor for the word
         return word;
      }
          
       public int Increment() {        //Essential mutator that increases the count
         count = count + 1;
         return 1;
      }
          
       public int Count() {            //Accessor for the count
         return count;
      }
   }


Actual program (ive written where im stuck)
CODE
// ********************************************************************************
*
//  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();
                    
                    //I'm stuck right here.......and am not sure if what I have is right.
              while ((next < LIMIT) && Stream.hasNext()){                    
                   WC[next] = //What should go here? ;
                    next = next + 1;
                                    }                                   
            }
      
             // 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");
         }
      }
   }


THANKS!






User is offlineProfile CardPM
+Quote Post


Gloin
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 03:54 PM
Post #2

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
WC[next] = //What should go here? ;

I'm guessing it should be..

WC[next] = Stream.next();

but what you have to do is save the word in a temporary string variable, check if it's in the array of words and if not, add it to the array, if it is, just increment the counter of the word..

ex..

String tmp = Stream.next();
for (int i= 0; i < WC.length; i++) {
if (tmp.equals(WC[i].Word()))
int redundant = WC[i].increment();
else
WC[WC.length] = new WordCount(tmp);
}

User is offlineProfile CardPM
+Quote Post

Jogan101
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 03:59 PM
Post #3

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 23

QUOTE(Gloin @ 16 Nov, 2008 - 03:54 PM) *

WC[next] = //What should go here? ;

I'm guessing it should be..

WC[next] = Stream.next();

but what you have to do is save the word in a temporary string variable, check if it's in the array of words and if not, add it to the array, if it is, just increment the counter of the word..

ex..

String tmp = Stream.next();
for (int i= 0; i < WC.length; i++) {
if (tmp.equals(WC[i].Word()))
int redundant = WC[i].increment();
else
WC[WC.length] = new WordCount(tmp);
}



I've tried the Stream.next() but the compiler gives me an incompatible types error.
WordCounterV1.java:42: incompatible types
found : java.lang.String
required: WordCount


User is offlineProfile CardPM
+Quote Post

Gloin
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 04:04 PM
Post #4

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
Yeah.. WC[i] is of type WordCounter and Stream.next is of type String. but as I mentioned later in my post, you shouldn't use the first piece of code..
User is offlineProfile CardPM
+Quote Post

Jogan101
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 04:18 PM
Post #5

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 23

QUOTE(Gloin @ 16 Nov, 2008 - 04:04 PM) *

Yeah.. WC[i] is of type WordCounter and Stream.next is of type String. but as I mentioned later in my post, you shouldn't use the first piece of code..



Thanks Gloin! I'm starting to get somewhere. I'll let you know how it goes.
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 04:22 PM
Post #6

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
Cool! smile.gif
User is offlineProfile CardPM
+Quote Post

Jogan101
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 04:36 PM
Post #7

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 23

Ok this may be a dumb question but what exactly does the "i" mean in...
WC[i]

User is offlineProfile CardPM
+Quote Post

Gloin
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 04:39 PM
Post #8

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
It was just there for indexation (instead of next), i being the first letter in the word index, guess it's the reason it's so commonly used in for-loops

This post has been edited by Gloin: 16 Nov, 2008 - 04:39 PM
User is offlineProfile CardPM
+Quote Post

Jogan101
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 04:48 PM
Post #9

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 23

QUOTE(Gloin @ 16 Nov, 2008 - 04:39 PM) *

It was just there for indexation (instead of next), i being the first letter in the word index, guess it's the reason it's so commonly used in for-loops


oh ok thanks!


User is offlineProfile CardPM
+Quote Post

Jogan101
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 05:02 PM
Post #10

New D.I.C Head
*

Joined: 16 Nov, 2008
Posts: 23

UGH! programming is so frustrating!
I can get the program to compile using this code

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();
            
               for (int i= 0; i < WC.length; i++) {
                  if (S.equals(WC[i].Word())){
                     int twin = WC[i].Increment(); // "twin" refers to a duplicate of the word
                  }
                  else{
                     WC[WC.length] = new WordCount(S);
                            
                  }
               }
            }
            // 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");
         }
      }
   }


But now what do I need to do...I'm just so lost...when I run the program I get an error Exception in thread "main" java.lang.NullPointerException
referring to the line: if (S.equals(WC[i].Word())){



User is offlineProfile CardPM
+Quote Post

Gloin
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 11:06 PM
Post #11

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
You may need to control that the array is not empty.

CODE


for (int i= 0; i < WC.length; i++) {
  if (S.equals(WC[i].Word())){
    int twin = WC[i].Increment(); // "twin" refers to a duplicate of the word
  }
  else{
    WC[WC.length] = new WordCount(S);
  }
}



In this loop, you check for each index of the array if its word is equal to S. If it's not then you create a new WordCount object. That means if you don't find the word then you have doubled the size of the array and half of it would contain the word S. What you need to do is, if you find the word, increment (this part is correct) but also use some flag-variable (boolean) that you shift if the word was found. Then after the loop has terminated, check the flag-variable and if it wasn't shifted, then add the new word. I.e, you should remove the else-part from inside the loop.

This post has been edited by Gloin: 16 Nov, 2008 - 11:19 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 11:22 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,951



Thanked: 673 times
Dream Kudos: 200
My Contributions
QUOTE(Gloin @ 16 Nov, 2008 - 04:39 PM) *

It was just there for indexation (instead of next), i being the first letter in the word index, guess it's the reason it's so commonly used in for-loops

He he
It is coming from good old Fortran

In Fortran variables didn't need to be initialized
you didn'y have so write

int i;

i was an int because: the type of the variable was determined by its first letter so INTEGER
all variables with names starting by I J K L M N were int the others where double/float

traditions persist... that is why you see

for(int i = 0; i < .....

more often than

for(int a = 0; a <



User is offlineProfile CardPM
+Quote Post

Gloin
RE: Help With Word Count Program Using Arrays
16 Nov, 2008 - 11:46 PM
Post #13

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
Cool, I had no idea.. smile.gif I'm still guessing they started on I (in Fortran) for the reason I claimed though.
User is offlineProfile CardPM
+Quote Post

PMJ
RE: Help With Word Count Program Using Arrays
18 Nov, 2008 - 11:11 AM
Post #14

New D.I.C Head
*

Joined: 18 Nov, 2008
Posts: 1

Don't even try it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:04PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month