Keep reading numbers until newline reached using a Scanner

前端 未结 2 2081
刺人心
刺人心 2021-02-06 13:13

I want to read a couple of numbers from the console. The way I wanna do this is by having the user enter a sequence of numbers separated by a space. Code to do the following:

2条回答
  •  清歌不尽
    2021-02-06 13:50

    You could use sc.nextLine() to read an input line, and then construct a new Scanner from that String (there is a constructor overload that takes a String) and read the ints from the second Scanner. (Note: If this is what you meant by "reading the whole line and the parse out the numbers", I'll delete this answer, but it looked like you were talking about manually reading the numbers with Integer.parseInt()).

提交回复
热议问题