Scanner input = new Scanner(System.in);
String s = input.next();
String[] x = s.split(" ");
//从键盘读取一个字符串,以空格分割,用x接受数组,
//但是x中只接受了第一个空格前的字符串

java字符串分割,接受不了数组
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- Crabime 2016-03-27 07:12关注
JSE中Scanner在API中有这么一段解释:"A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods."意思就是说默认的中断输入方式为空格,所以你这里很显然用空格的方式去获取String所对应数组的方式是错误的,你也可以显示改变分隔符,在Scanner对象下面增加:input.useDelimiter("\r");其它代码不变,可以达到你的目的。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报