Help With Word Count Program using Arrays

I'm stuck on a few parts of my program...

Page 1 of 1

13 Replies - 4925 Views - Last Post: 18 November 2008 - 12:11 PM Rate Topic: -----

#1 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Help With Word Count Program using Arrays

Post icon  Posted 16 November 2008 - 04:39 PM

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
//*********************************************************************************
//  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)
//*********************************************************************************
//  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!

Is This A Good Question/Topic? 0
  • +

Replies To: Help With Word Count Program using Arrays

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 04: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);
}
Was This Post Helpful? 0
  • +
  • -

#3 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 04:59 PM

View PostGloin, on 16 Nov, 2008 - 03:54 PM, said:

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
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 05: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..
Was This Post Helpful? 1
  • +
  • -

#5 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 05:18 PM

View PostGloin, on 16 Nov, 2008 - 04:04 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 05:22 PM

Cool! :)
Was This Post Helpful? 0
  • +
  • -

#7 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 05:36 PM

Ok this may be a dumb question but what exactly does the "i" mean in...
WC[i]
Was This Post Helpful? 0
  • +
  • -

#8 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 05: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

This post has been edited by Gloin: 16 November 2008 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 05:48 PM

View PostGloin, on 16 Nov, 2008 - 04:39 PM, said:

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!
Was This Post Helpful? 0
  • +
  • -

#10 Jogan101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 16-November 08

Re: Help With Word Count Program using Arrays

Posted 16 November 2008 - 06:02 PM

UGH! programming is so frustrating!
I can get the program to compile using this 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())){
Was This Post Helpful? 0
  • +
  • -

#11 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help With Word Count Program using Arrays

Posted 17 November 2008 - 12:06 AM

You may need to control that the array is not empty.


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: 17 November 2008 - 12:19 AM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help With Word Count Program using Arrays

Posted 17 November 2008 - 12:22 AM

View PostGloin, on 16 Nov, 2008 - 04:39 PM, said:

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 <
Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help With Word Count Program using Arrays

Posted 17 November 2008 - 12:46 AM

Cool, I had no idea.. :) I'm still guessing they started on I (in Fortran) for the reason I claimed though.
Was This Post Helpful? 0
  • +
  • -

#14 PMJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-November 08

Re: Help With Word Count Program using Arrays

Posted 18 November 2008 - 12:11 PM

Don't even try it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1