将stdin输入转换为Java整数数组

问题描述 投票:0回答:1

我正在尝试将来自扫描仪的用户输入读取为整数数组,我现在有这个:

int [] arr1 = {Integer.parseInt(sc.nextLine().split(" "))};

但是我得到了错误

String[] cannot be converted to String

Any help would be much appreciated :)
java arrays java.util.scanner
1个回答
0
投票
sc.nextLine().split(" ") //This returns String[]
int a = Integer.parseInt("") //Integer.parseInt requires one String param.

尝试以下代码:

String input = sc.nextLine();
String[] inputs = input.split(" ");

List<Integer> ints = Arrays.asStream(inputs).
map(Integer:parseInt).collect(Collectors.toList());

检查文件:API:Integer.parseIntString.split()

© www.soinside.com 2019 - 2024. All rights reserved.