How to read multiple integer values from one line in Java using BufferedReader object?
Solution 1
Try the next:
int a[] = new int[n];
String line = br.readLine(); // to read multiple integers line
String[] strs = line.trim().split("\\s+");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(strs[i]);
}
Solution 2
Late to the party but you can do this in one liner in Java 8 using streams
.
InputStreamReader isr= new InputStreamReader();
BufferedReader br= new BufferedReader(isr);
int[] input = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
Solution 3
If you want to read integers and you didn't know number of integers
String[] integersInString = br.readLine().split(" ");
int a[] = new int[integersInString.length];
for (int i = 0; i < integersInString.length; i++) {
a[i] = Integer.parseInt(integersInString[i]);
}
Solution 4
Integer.parseInt(br.readLine())
-> Reads a whole line and then converts it to Integers.
scanner.nextInt()
-> Reads every single token one by one within a single line then tries to convert it to integer.
String[] in = br.readLine().trim().split("\\s+");
// above code reads the whole line, trims the extra spaces
// before or after each element until one space left,
// splits each token according to the space and store each token as an element of the string array.
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(in[i]);
// Converts each element in the string array as an integer and stores it in an integer array.
Solution 5
I use this code for List:
List<Integer> 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();
![Lalit Kumar](https://i.stack.imgur.com/dtMWX.jpg?s=256&g=1)
Lalit Kumar
Updated on July 27, 2022Comments
-
Lalit Kumar almost 2 years
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 read all these data in an integer array.
Input format- the user first enters how many numbers he/she want to enter
And then multiple integer values in the next single line-
INPUT:
5
2 456 43 21 12
Now, I read input using an object (br) of BufferedReader
int numberOfInputs = Integer.parseInt(br.readLine());
Next, I want to read next line inputs in an array
int a[] = new int[n];
But we cannot read using this technique
for(int i=0;i<n;i++) { a[i]=Integer.parseInt(br.readLine()); //won't work }
So, is there any solution to my problem or we can't just read multiple integers from one line using BufferedReader objects
Because using Scanner object we can read this type of input
for(int i=0;i<n;i++) { a[i]=in.nextInt(); //will work..... 'in' is object of Scanner class }
-
Paul Vargas almost 10 years@LalitChoudhary You're welcome. Do not forget that if an answer solves your question satisfactorily, you can accept it.
-
cezar over 5 yearsPlease add some explanation. The answer should be useful to a broader public, not just to the OP.