I am solving a task, but stuck in one moment with input numbers.
Input example:
//n and k
3 2
// inputing random n numbers (this time 3 numbers)
2 3 4
In the first line I have 2 digits (n and k seperated by space). In the second line I need to input n numbers (which is given in the fist line) seperated by space . How can I solve this? This is what I have now.
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String lines = br.readLine();
String[] strs = lines.trim().split("\s+");
int n = Integer.parseInt(strs[0]);
int k = Integer.parseInt(strs[1]);
//here starts my problem
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < n; i++) {
int number = Integer.parseInt(br.readLine());
numbers.add(number);
}
}
I managed to take the first two digits in first line. But I couldn't solve taking n digits of the second line. I need it to be int. Thanks for the assistance.
question from:
https://stackoverflow.com/questions/65905994/exactly-n-numbers-in-a-row 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…