This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes count number of spaces... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "count number of spaces..." Watch "count number of spaces..." New topic
Author

count number of spaces...

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
Is there some sort of a command that will cound the amount of space in between strings? I'm using a tokenizer to extract strings from a line in a file and I want to keep something of a column count, that gives the placement of the string within the line.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interesting problem awright. A few ideas...
Convert your string to char array and inspect one character at a time. Build up consecutive non-blanks into words, count consecutive blanks or just save the index at the start of each word. Sounds a little finicky.
If you have JDK 1.4 or later, look into the Pattern and Match classes and regular expressions. You might be able to put blanks into a group that you can retrieve and inspect. I can't swear that will work. Ask for details if it made no sense.
Use tokenizer to get all the words, then use indexOf to get the starting point of each word in the big string. Be careful to use the "from" option in indexOf so you don't confuse words with each other.
Do any of those ideas sing to you?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tom Wolve
Ranch Hand

Joined: Sep 22, 2003
Posts: 32
Hi,
if you're using a StringTokenizer with ' ' as delimiter character, use it with returnDelims enabled.
Count all tokens which are equal to " " and that should do it.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
D'oh! Excellent! I have forgotten that one at some expense before. Shame on me!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: count number of spaces...
 
Similar Threads
startsWith and prefix finders
Using commandline arguments Query
identifier expected
Counting .executeBatch();
C2143 error and I can't find it