wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Knowing how many words are in each string sentence? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Knowing how many words are in each string sentence?" Watch "Knowing how many words are in each string sentence?" New topic
Author

Knowing how many words are in each string sentence?

vahid mirjamal
Greenhorn

Joined: Feb 24, 2009
Posts: 9
I've been working all day on trying to get a paragraph string, and finding out how many sentences it has, how many words it has in each sentence, and which words are small words (below 4 letters.)

I'm totally not getting it.



Any hints? I'm totally stuck at a dead end and i've been sitting here trying to figure this out for around.. 5 hours so far non stop with searching and so far. I believe it needs loops to determine most of these but i just don't know where to start.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

Any hints? I'm totally stuck at a dead end and i've been sitting here trying to figure this out for around.. 5 hours so far non stop with searching and so far. I believe it needs loops to determine most of these but i just don't know where to start.



What else have you tried during those 5 hours? Maybe some of the ideas that you had, or code that you attempted, and failed, where actually good ideas, and we can give you a hint in the right direction.

After 5 hours, you must have tons of questions to ask.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vahid mirjamal
Greenhorn

Joined: Feb 24, 2009
Posts: 9
Hehe your so right, I'm starting to go back and see my old code and it's not all bad. Like I got here so far:



But for for some reason it counts the extra spaces as an extra word, how can i get it to stop counting the extra space? If i figure that out I should be closer to figuring this out. I got it to exclude double spaces, etc from my first regex:


But it doesn't seem to be working in the counting words one..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

But for for some reason it counts the extra spaces as an extra word, how can i get it to stop counting the extra space? If i figure that out I should be closer to figuring this out. I got it to exclude double spaces, etc from my first regex:

But it doesn't seem to be working in the counting words one..


The extra space is probably in the beginning of the sentence -- as people generally put a space after the period from the previous sentence. You may want to consider trimming the sentence before splitting it.

Another option is to loop through the word list (after the sentence is split) and check to make sure that every word has at least 1 character. If it is splitting an extra word due to an extra space, the extra word should be a zero-length string.

Henry
vahid mirjamal
Greenhorn

Joined: Feb 24, 2009
Posts: 9


Like that?

Because it's not exactly working :|
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

Like that?


Actually, no.... I suggested that you trim the sentence. Not the paragraph. (And it was only speculation, as you never showed us the actual symtoms).

I also suggested that you count the words -- by checking if the length is greater than zero -- which you didn't even attempt.

Because it's not exactly working :|


Statements like "it's not exactly working" is completely useless here. You need to describe what you did. what you expected, what you got, and.... You need to attempt to debug the code to try to isolate where the problem is. Or at least speculate.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Knowing how many words are in each string sentence?
 
Similar Threads
Word count problem.
Problems with counting short and long words
Loop Problem
Please help on Strings
How Elegant Is This Solution? Can You Offer Me Recommendations?