【发布时间】:2012-11-07 07:58:10
【问题描述】:
我想将像“我的狗”这样的字符串拆分成一个数组:
| M | y | space char will be in here | D | o | g |
这是我的代码:
String []in_array;
input = sc.next();
in_array = input.split(""); //Note this there is no delimiter
for(int k=1; k < in_array.length; k++){
System.out.print(" "+in_array[k]);
}
编辑:
它只打印出“我的”
【问题讨论】:
-
那么,发生了什么?
-
问题是什么?它奏效了吗?如果没有,它做错了什么?
-
嗯,在我的电脑上,它会打印出
My Dog。请重新测试一下。您的输入是否真的包含:-My Dog? -
@RohitJain 查看我的编辑。我使用的是扫描仪。
-
@AdegokeA.. 使用scanner.next() 方法仅读取下一个令牌。因此,一旦遇到
space,它就会停止读取。