How to read multiple integer values from one line in Java using BufferedReader object?

前端 未结 9 2245
萌比男神i
萌比男神i 2021-02-06 15:39

I am using BufferedReader class to read inputs in my Java program. I want to read inputs from a user who can enter multiple integer data in single line with space. I want to rea

9条回答
  •  忘了有多久
    2021-02-06 16:17

    I use this code for List:

    List numbers = Stream.of(reader.readLine().split("\\s+")).map(Integer::valueOf).collect(Collectors.toList());
    

    And it is almost the same for array:

    int[] numbersArray = Arrays.stream(reader.readLine().split("\\s+")).mapToInt(Integer::valueOf).toArray(); 
    

提交回复
热议问题