「状態」を表すブール値を維持できます。スペースのブロックに入っていますか (true)、またはスペース以外のブロックに入っていますか (false)?
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
String WORDS_GROUP = ",\n ";
String text = "This is my sample text";
int wordCount = 0;
boolean previousCharWasSpace = true;
for (int i = 0; i < text.length(); i++) {
boolean thisCharIsASpace = false;
for (int j = 0; j < WORDS_GROUP.length(); j++) {
if (text.charAt(i) == WORDS_GROUP.charAt(j)) {
previousCharWasSpace = true;
thisCharIsASpace = true;
break;
}
System.out.println("char=" + text.charAt(i) + " j=" + j
+ " previousCharWasSpace=" + previousCharWasSpace);
}
if (!thisCharIsASpace && previousCharWasSpace) {
wordCount++;
previousCharWasSpace = false;
System.out.println("char=" + text.charAt(i)
+ " previousCharWasSpace=" + previousCharWasSpace
+ " wordCount=" + wordCount);
}
}
System.out.println("wordCount=" + wordCount);
}
}
出力:
char=T j=0 previousCharWasSpace=true
char=T j=1 previousCharWasSpace=true
char=T j=2 previousCharWasSpace=true
char=T previousCharWasSpace=false wordCount=1
char=h j=0 previousCharWasSpace=false
char=h j=1 previousCharWasSpace=false
char=h j=2 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=false
char=i j=1 previousCharWasSpace=false
char=i j=2 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=true
char=i j=1 previousCharWasSpace=true
char=i j=2 previousCharWasSpace=true
char=i previousCharWasSpace=false wordCount=2
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=true
char=m j=1 previousCharWasSpace=true
char=m j=2 previousCharWasSpace=true
char=m previousCharWasSpace=false wordCount=3
char=y j=0 previousCharWasSpace=false
char=y j=1 previousCharWasSpace=false
char=y j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=true
char=s j=1 previousCharWasSpace=true
char=s j=2 previousCharWasSpace=true
char=s previousCharWasSpace=false wordCount=4
char=a j=0 previousCharWasSpace=false
char=a j=1 previousCharWasSpace=false
char=a j=2 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=false
char=m j=1 previousCharWasSpace=false
char=m j=2 previousCharWasSpace=false
char=p j=0 previousCharWasSpace=false
char=p j=1 previousCharWasSpace=false
char=p j=2 previousCharWasSpace=false
char=l j=0 previousCharWasSpace=false
char=l j=1 previousCharWasSpace=false
char=l j=2 previousCharWasSpace=false
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char=t j=0 previousCharWasSpace=true
char=t j=1 previousCharWasSpace=true
char=t j=2 previousCharWasSpace=true
char=t previousCharWasSpace=false wordCount=5
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char=x j=0 previousCharWasSpace=false
char=x j=1 previousCharWasSpace=false
char=x j=2 previousCharWasSpace=false
char=t j=0 previousCharWasSpace=false
char=t j=1 previousCharWasSpace=false
char=t j=2 previousCharWasSpace=false
wordCount=5
ここでこれで遊ぶことができます: http://ideone.com/ASEjwB