您的问题是(显然)默认情况下将空格视为分隔符(不足为奇);您已将逗号设置为空白字符,因此逗号和空格都被视为空白字符。下面的程序做我认为你想要的;注意将空格设置为“wordChars”的行。
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
public class TokenTeaser
{
public static void main(String[] args)
{
try
{
String testString = "one two, three, four five";
StringReader sr = new StringReader(testString);
StreamTokenizer st = new StreamTokenizer(sr);
st.whitespaceChars(',', ',');
st.wordChars(' ', ' ');
int currentToken = st.nextToken();
while (currentToken != StreamTokenizer.TT_EOF)
{
System.out.println(st.sval);
currentToken = st.nextToken();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}