Can you help me?

I am pretty much a newbie here so please be patient with me. I am writing code for an assignment I have in which we are supposed to use the StringTokenizer class. The assignment asks to create a class that provides several methods that determine and return several different characteristics, i.e. # of sentences in a string, # of words in entire string, length of the longest word (in characters) & finally length of the longest sentence (in words). Now I was able to figure all these out EXCEPT the last one, length of the longest sentence using StringTokenizer. I was able to find out what the length of each sentence is in characters but I need it in words, Can anyone help me? Here is what I have so far:

StringTokenizer MaxSent = new StringTokenizer(Passage,".");

while (MaxSent.hasMoreTokens())

{

String max = MaxSent.nextToken();

System.out.println("longest: " + max);

}

This prints out each sentence seperately but how can I count how many space there are in each part to give me the longest sentence? I tried using a " " (space) token but it wont work.

Please please, someone help me out my assignment is due today and I have been busting my head for a week trying to figure this out.

Thanks!!!!

[1269 byte] By [Lou_100] at [2007-9-27 22:49:12]
# 1
You do need help. A whole week of work and you've got five lines of code.This will help http://search.java.sun.com/search/java/index.jsp?qp=&nh=10&qt=%2BStringTokenizer+%2Bsentences+%2Bwords+&col=javaforums&x=36&y=12
bbritta at 2007-7-7 13:54:57 > top of Java-index,Archived Forums,Java Programming...
# 2

Well, think about how you would do it if you were doing it by hand, if you had a paragraph of sentences. You would go through the sentences one-by-one, counting the words. What makes a sentence? A period (or '?' or '!' don't forget... unless you were told to only worry about periods). You've seemed to be able to break the String into sentences fine. So, now, during each iteration of your while loop, you have a String that represents a sentence. You can use the same type of technique you did to determine what block characters were sentences. The only differences are that instead of using the whole String to find the words, you're using just that sentence and that instead of looking for the punctuation as your delimiter, you're looking for spaces. (the countTokens() method of StringTokenizer may be of interest to you here.)

Sorry if I was so cryptic in the reply... I'd much rather help someone to learn how to think through the problem than giving them the answer (it really will help you in the long run). If you're still having a hard time, post back with what you've come up with.

Brian-74-454 at 2007-7-7 13:54:57 > top of Java-index,Archived Forums,Java Programming...
# 3

I have tried that logic more times that I can think of and this is what I come up with:

StringTokenizer Sent = new StringTokenizer(Passage,".");

StringTokenizer wrd = new StringTokenizer(Passage," ");

while (Sent.hasMoreTokens())

{

String max = Sent.nextToken();

MaxSent = wrd.countTokens();

System.out.println("Sentences: " + max);

System.out.println("Spaces: " + MaxSent);

}

However when it prints it gives me the # of spaces in the entire string rather than just that 1 sentence.

Lou_100 at 2007-7-7 13:54:57 > top of Java-index,Archived Forums,Java Programming...
# 4

What you need to do is to count just the spaces in the sentence, when you have the line "StringTokenizer wrd = new StringTokenizer(Passage," ");" you are creating a tokenizer for the whole passage, you just want one for a particular sentence. Try creating a new tokenizer everytime you find a new sentence.

Brian-74-454 at 2007-7-7 13:54:57 > top of Java-index,Archived Forums,Java Programming...
# 5
I got it!!!!! Thanks sooo much for your help!!! I needed a little outside logic.
Lou_100 at 2007-7-7 13:54:57 > top of Java-index,Archived Forums,Java Programming...