【发布时间】:2019-02-19 18:09:32
【问题描述】:
我正在尝试用空格分割用户条目并将其存储为数组。我正在尝试打印出每个索引中的所有元素,但我收到一个错误提示索引超出范围。
例如,我想打印出"Hello world",我得到一个超出范围的异常:
Scanner scan = new Scanner(System.in);
String command = scan.next();
String[] result = command.split(" ");
System.out.print(result[0]);
System.out.print(result[1]);
【问题讨论】:
-
Scan.next() 只消耗一个令牌。使用 nextLine()