Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Counting words in a String

 
Mitch Kazlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have to pass a string to a method and return the count of words within the String. The issue I'm having is how to ignore ALL whitespace while counting the words.
While doing some searching I see that most people are using a String array to count words so that is what I have written so far.

My questions are:

1) Can I do this without using an array and just using a for loop / if else combination?

2) How do I dump All of the whitespace. My array solution appears to work fine until the # of spaces between the words increases.

Thanks..

 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On your original string, first call trim() to remove trailing/heading spaces, then you can use the replaceAll method on your trimmed string to replace multiple spaces into single spaces. Finally do the split the way you're currently doing.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mitch Kazlo wrote:
1) Can I do this without using an array and just using a for loop / if else combination?


What would be the advantage of that?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12198
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it would probably make more sense to split your string using a regular expression...
 
Mitch Kazlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help! Solved it with your advice Koen.

Jeff,

I was wondering if I could do it with a loop and if / else because we haven't been working with arrays,
but everything I came across on-line seemed to use them for this purpose.

-M
 
Mitch Kazlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woops... I spoke too soon. Thought I had it.. This is where I am. If I pass in a string with a lot of spaces this isn't working. This will return 14 instead of the correct work count 6.

 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your regular expression is wrong. It only replaces 2 spaces by one; instead you want something more generic that replace ANY number of 1 or more spaces by just one space. Look into regular expression characters like ? and *

Also, you're not assigning the results of the replaceAll method to anything, for instance str = str.replaceAll(); You can also combine method calls on one line like this: String[] result = mystring.trim().replaceAll().split().
 
Mitch Kazlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again.. I found the character I needed..

-M
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:it would probably make more sense to split your string using a regular expression...


Yes, you could use a short hand character like \s and replace 1 or many space occurrences with just 1 space.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:On your original string, first call trim() to remove trailing/heading spaces, then you can use the replaceAll method on your trimmed string to replace multiple spaces into single spaces. Finally do the split the way you're currently doing.


Praveen Kumar M K wrote:
Yes, you could use a short hand character like \s and replace 1 or many space occurrences with just 1 space.


There's no need for any replaceAll() step. Just trim() then split() it all that's necessary. (Or, I suppose, you skip the trim() and do a single replaceAll() followed by split().)
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
There's no need for any replaceAll() step. Just trim() then split() it all that's necessary. (Or, I suppose, you skip the trim() and do a single replaceAll() followed by split().)


Since the OP doesn't care about the results of the split(), only about the count, there is no need for the trim() or the replaceAll().




Heck, using the same argument, there is a lot that can be done to optimize the regex too.



Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Since the OP doesn't care about the results of the split(), only about the count


Oops. Missed that part. Guess I need to learn to ReadTheQuestion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic