How to take multiple integer input in one line using BufferedReader in Java?

+1 vote
Please help me to solve the problem of taking multiple integer input in a single line using BufferedReader class?

In Scanner class we can take multiple input like all elements of array in one line. How to do that using BufferedReader?
Mar 4, 2019 in Java by Shruti
5,485 views

2 answers to this question.

0 votes

According to me what you can do is take the input as a string and then divide it accordingly. If you want the input to be Integer then you can typecast it. Here is a small example for taking the input of an array in one line.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
int num = Integer.parseInt(br.readLine()); //Length of Array
String s= br.readLine(); //Input the number seperated by space
int[] arr= new int[num];
String[] s1 = s.split(" ");
for(int i=0;i<num;i++)
{
    arr[i]=Integer.parseInt(s1[i]);
}

This will work.

answered Mar 4, 2019 by Priyaj
• 57,450 points
no working ..i cant understand this useless code....yopur comment written on the code useless this confuses a person more than he already was
The other answer works fine
0 votes

Try this in case you don't know the 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]);
}
answered Aug 29, 2019 by Sirajul
• 44,350 points

Related Questions In Java

0 votes
2 answers

How to call one constructor from another in Java?

public class Cons { public static Cons ...READ MORE

answered Jul 24, 2018 in Java by Akrati
• 3,150 points
292 views
0 votes
1 answer

How to encode data using Base64 in Java?

import org.apache.commons.codec.binary.Base64; We can not use sun.* packages ...READ MORE

answered May 30, 2018 in Java by Sushmita
• 6,880 points
80 views
0 votes
1 answer

How to pad an integer with zeros on the left in Java?

Use java.lang.String.format() method. String.format("%05d", number) ...READ MORE

answered May 31, 2018 in Java by Daisy
• 8,080 points
337 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System.o ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 44,350 points
323 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,540 points
87 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
84 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,660 points
618 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
950 views
0 votes
2 answers

How to take input using BufferedReader in Java?

Reading User's Input using BufferedReader class: By wrapping ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 44,350 points
2,371 views
+15 votes
18 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 700 points
103,655 views