Im having trouble figuring out this one.... Im albe to cound the number of words and characters in a sentence.. but how would I go about actually counting the number of times a particular word appears in a sentence..
//Declare the sentence and word String sentence = JOptionPane.showInputDialog(null, "Enter sentence: "); String word = JOptionPane.showInputDialog(null, "Enter word: ");
// Declare the number of characters in sentence int numberOfCharactersInSentence = sentence.length(); System.out.println("This is the number of characters in the " + "sentence " + numberOfCharactersInSentence);
int tempWordCount = 0; char wordArray[] = new char[tempWordCount];
//initiate and create new array of characters in sentence char characterArrayOfSentence[] = new char[numberOfCharactersInSentence];
//-------------------------- // COUNT THE NUMBER OF WORDS //-------------------------- int w=1; for(int i=0; i<sentence.length(); i++){ if(sentence.charAt(i)==' '){ w++; } } System.out.println("The number of words in the sentence is: " + w); } }
I'd be very greatful with any help!! Thanks so much -Kath