Hello everyone,
I've recently been introduced to arrays in my java module and i'm stuck on a question:
Write a program which reads a sequence of words, and prints a count of the number of distinct words. An example of input/output is
should I stay or should I go
5 distinct words
Could anybody explain to me how I would go about checking the array to see if each word has occurred already? Is linear searching the best route to take? Thanks for any help given. Oh and class Console is just a more convenient version of ConsoleReader()
Even though this is probably all wrong, here is my attempted code so far:
public class DistinctWords { public static void main(String[] args) { String[] words = new String[10]; // Allow up to 10 words int count=0; int distinctWord=0; System.out.println("Enter sentence:"); while(!Console.endOfFile()) { words[count]=Console.readToken(); count++; } int i=1; while(i<count && !words[i].equals(words[i-1])) { i++; } if(i<count) // found System.out.println("Found"); }//end main }//end class