java字符串分割,接受不了数组

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

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");其它代码不变,可以达到你的目的。

sanner默认以空格、tab、回车作为分隔符;
.next()方法按分隔符一次读取一个,所以这样写只能获取第一个元素。
建议这样改一下试试:
String s = input.nextLine();

是"\r",我copy到这个上面就突然编程"\r"了

CSDN有bug,是双反斜杠,单斜杠会报错

哇,5年了,我找回了密码。感谢各位大佬的回复~