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

我正在尝试将用户输入从扫描仪读取到整数数组中,目前有:

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

但是我得到了错误

String[] cannot be converted to String

Any help would be much appreciated :)
评论
  • 妖小精
    妖小精 回复

    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());
    
    

    Check the document:
    API: Integer.parseInt String.split()