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
133 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,120 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
72 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,820 points
31 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,020 points
44 views
0 votes
1 answer

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

you want to use the Arrays.toString(int[]) method: import java.util.Arrays; int[] array ...READ MORE

answered Jun 7, 2018 in Java by Akrati
• 3,150 points
110 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,520 points
34 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
22 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
201 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
100 views
0 votes
1 answer

How to take input using BufferedReader in Java?

BufferedReader is used to decrease the time ...READ MORE

answered Mar 4 in Java by Priyaj
• 56,120 points
21 views
+14 votes
17 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
52,464 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.