I am working on a program and I want to allow a user to enter multiple integers when prompted. I have tried to use a scanner but I found that it only stores the first integer entered by the user. For example:
Enter multiple integers: 1 3 5
The scanner will only get the first integer 1. Is it possible to get all 3 different integers from one line and be able to use them later? These integers are the positions of data in a linked list I need to manipulate based on the users input. I cannot post my source code, but I wanted to know if this is possible.
I use it all the time on hackerearth
Using this on many coding sites:
Suppose you are given 3 test cases with each line of 4 integer inputs separated by spaces
1 2 3 4
,5 6 7 8
,1 1 2 2
CASE 2: WHEN NUMBER OF INTEGERS in each line is NOT GIVEN
Note that you need to trim() first:
trim().split("\\s+")
- otherwise, e.g. splittinga b c
will emit two empty strings first