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!
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...
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"); } } }
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); }
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
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..
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.
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
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
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"); } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> }
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())){
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
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