Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我能';t在java中使用StringTokenizer将字符串拆分为令牌_Java_Stringtokenizer - Fatal编程技术网

我能';t在java中使用StringTokenizer将字符串拆分为令牌

我能';t在java中使用StringTokenizer将字符串拆分为令牌,java,stringtokenizer,Java,Stringtokenizer,我正在尝试使用StringTokenizer将字符串拆分为令牌 我的代码是: String original = "Short stories for kids are adventurous and interesting ways to teach your children about good morals and right conduct."; String delimiters = "+-*/(),.? "; StringTokenizer st = new

我正在尝试使用StringTokenizer将字符串拆分为令牌

我的代码是:

    String original = "Short stories for kids are adventurous and interesting ways to teach your children about good morals and right conduct.";
    String delimiters = "+-*/(),.? ";
    StringTokenizer st = new StringTokenizer(original, delimiters);
    while (st.hasMoreTokens()) {
    String w = st.nextToken();
    System.out.println("tokens are = = = "+w);
    }
运行此代码时,输出为:

token are = = = Short
token are = = = stories
token are = = = for
token are = = = kids are
token are = = = adventurous
token are = = = and
token are = = = interesting
token are = = = ways
token are = = = to
token are = = = teach
token are = = = your
token are = = = children
token are = = = about
token are = = = good
token are = = = morals
token are = = = and
token are = = = right
token are = = = conduct
我使用空格
(+-*/(),.?)
作为分隔符。 有一个词叫
孩子们是
。我对输出感到困惑。但它是一种被嵌入的输出。。。!!!
事实上,我相信输出的是两个单词作为分隔符(分隔符)


为什么我会得到这种类型的输出?

您使用的是哪种版本的Java

使用Java7进行了尝试,它似乎打印出了正确的输出

以这种形式输出:

tokens are = = =Short
tokens are = = =stories
tokens are = = =for
tokens are = = =kids
tokens are = = =are
tokens are = = =adventurous
tokens are = = =and
tokens are = = =interesting
tokens are = = =ways
tokens are = = =to
tokens are = = =teach
tokens are = = =your
tokens are = = =children
tokens are = = =about
tokens are = = =good
tokens are = = =morals
tokens are = = =and
tokens are = = =right
tokens are = = =conduct

它应该可以工作,你确定在
孩子
之间没有空格而没有其他空格吗?我已经试过你的代码了,它工作得很好。“kids”和“are”之间必须有不同的空格,我将获得预期的输出。您正在使用哪个Java版本?同样出于好奇,您发布的是程序的输出吗?输出中的“=”和令牌之间有一个空格,但代码是System.out.println(“令牌是==”+w);这不会给任何空间。你好@Marcinoskolowski,我找不到你。空格和空白之间的区别是什么?您好@gokkk,我也在使用Java7.0。同样,字符串是按照gokkk描述的方式分割的。