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 5, 2019 in Java by Shruti
98,166 views

4 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.

For more take Java certification class now and become the expert.

answered Mar 5, 2019 by Priyaj
• 58,020 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
• 59,230 points
Thanks it works.
0 votes

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]);
}
answered Dec 14, 2020 by Gitika
• 65,770 points
0 votes

To do this, we could read in the user’s input String by wrapping an InputStreamReader object in a BufferedReader object.

Then, we use the readLine() method of the BufferedReader to read the input String – say, two integers separated by a space character. These can be parsed into two separate Strings using the String.split() method, and then their values may be assigned to a and b, using the parseInt method as you suggest above.

(We would likely want to verify the user input as well to ensure it is in the desired format, but that issue hasn’t been addressed here.)

First, remember to import java.io 58.* for the Reader objects. The readLine() method throws an IOException, so the Main method can go like this:

public static void main(String[] args) throws IOException 
{ 
BufferedReader in = new BufferedReader( 
new InputStreamReader( 
System.in)); 

String[] input = new String[2]; 
int a; 
int b; 

System.out.print("Please enter two integers: "); 
input = in.readLine().split(" "); 

a = Integer.parseInt(input[0]); 
b = Integer.parseInt(input[1]); 

System.out.println("You input: " + a + " and " + b); 
} 

------------- ADDITIONAL INFO --------------

I forgot about the Scanner object. This can make things a lot simpler. Instead of importing the Reader objects, we import java.util.Scanner

The Scanner object can parse user input directly, so we don’t have to split Strings or use parseInt. Also, we don’t necessarily need to worry about catching an IOException. And, the user may input both integers on the same line, or even on different lines, as desired.

Using the Scanner object, our much simpler main function can go like this:

public static void main(String[] args) 
{ 
System.out.print("Please enter two integers: "); 

Scanner sc = new Scanner(System.in); 

int a = sc.nextInt(); 
int b = sc.nextInt(); 

System.out.println("You input: " + a + " and " + b); 
}
answered Dec 14, 2020 by Roshni
• 10,480 points

Related Questions In Java

0 votes
1 answer

How to take character input in Java?

In Java, you can take character input ...READ MORE

answered Oct 19, 2023 in Java by anonymous
• 3,360 points

edited Oct 19, 2023 by anonymous 725 views
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,190 points
1,572 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,920 points
1,071 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,140 points
2,285 views
+1 vote
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,620 points
1,337 views
+1 vote
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,290 points
1,197 views
+1 vote
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,680 points
3,759 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,220 points
3,608 views
+1 vote
5 answers

How to take input using BufferedReader in Java?

yupp i am explaining every thing .. We ...READ MORE

answered Feb 1, 2020 in Java by Rohan
• 180 points
123,851 views
+17 votes
25 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
• 720 points
253,717 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP