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

0 votes
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 in Java by Shruti
1,340 views

1 answer 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 by Priyaj
• 56,520 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
125 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,870 points
42 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,050 points
103 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 in Java by Sirajul
• 19,670 points
191 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
52 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
42 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,320 points
349 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
264 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 in Java by Sirajul
• 19,670 points
60 views
+14 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 in Java by Suresh
• 700 points
72,641 views