The Artima Developer Community
Sponsored Link

Java Answers Forum
Display the number of times a particular word appears in a sentence

3 replies on 1 page. Most recent reply: Aug 21, 2006 7:20 AM by Jagdish

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
kat ta

Posts: 6
Nickname: codekat
Registered: Apr, 2006

Display the number of times a particular word appears in a sentence Posted: Jun 3, 2006 7:39 PM
Reply to this message Reply
Advertisement
hello all again!

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..


package examQuestion13;

import javax.swing.*;

public class CountingWords {


//-----------------------------------
// DECLARATIONS
//-----------------------------------


public static void main(String[] args) {

//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


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Display the number of times a particular word appears in a sentence Posted: Jun 5, 2006 8:05 AM
Reply to this message Reply
This appears to be coursework. Therefore, I will only provide hints.

java.util.Map

kat ta

Posts: 6
Nickname: codekat
Registered: Apr, 2006

Re: Display the number of times a particular word appears in a sentence Posted: Jun 5, 2006 5:13 PM
Reply to this message Reply
Thanks I've got it to work!

Jagdish

Posts: 1
Nickname: jag
Registered: Apr, 2003

Re: Display the number of times a particular word appears in a sentence Posted: Aug 21, 2006 7:20 AM
Reply to this message Reply
And also use StrinkTokenizer or String.split() method(supported by only after 1.4)

Flat View: This topic has 3 replies on 1 page
Topic: Hw to add two no's without + sign Previous Topic   Next Topic Topic: Rookie question on Boolean operators

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use