Piyush Vishwakarma
Piyush Vishwakarma

Reputation: 1826

Multiple integer read in java with bufferedreader in java

I working on a program where I have to read about 10^6 integers. I tried working with the scanner class however when i tried for 800000*3 inputs, it took around 12.38 seconds. I also tried tried to work with the BufferedReader which actually worked faster but then it does not take the input i give as desired.

For e.g. if I want to read 3 numbers separated with a space, three consecutive nextInt() would work, but such is not the case for BufferedReader, it accepts the space as a string and while parsing the string into integer throws NumberFormatException exception.

input e.g. "8347 394730 3487", all three numbers must be stored separately.

code e.g

public static void main(String[] args) throws IOException
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String b=br.readLine();     
    int x=Integer.parseInt(b);
    b=br.readLine();        
    int x1=Integer.parseInt(b);
    b=br.readLine();        
    int x2=Integer.parseInt(b);
    System.out.println(x+x1+x2);
}

Also the numbers can be as large as 10^10.

So I need help in using BufferedReader for such input. Also if at all there is any other alternate but faster method for reading integers, will be good enough.

Thank you

Upvotes: 1

Views: 14331

Answers (5)

Stephen C
Stephen C

Reputation: 718826

The String.split(separator) method can be used to split a string into an array of strings, with the separator regex providing the separator. Refer to the String and Pattern javadoc for the complete description.


However, if you are really concerned about performance, you could potentially reduce the time even further by using String.indexOf and String.substring to extract the numbers for parsing. Or potentially parse them yourself to avoid the overhead of creating strings.

Upvotes: 0

Mehdi
Mehdi

Reputation: 3763

get the String and then use this :

String[] numberList = yourString.split("\\s+");
List<Integer> myList = new ArrayList<Integer>();
for(String num : numberList){
     myList.add(Integer.parseInt(num));
}

update* : please try this one

public class Answer {

public static void main(String[] args) throws Exception {
    List<String> eachLineList = new ArrayList<String>();

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String b = br.readLine();
    eachLineList.add(b.trim()); //Line 1 added to String list

    b = br.readLine();
    eachLineList.add(b.trim()); //Line 2 added to String list

    b = br.readLine();
    eachLineList.add(b.trim()); //List 3 added to String list

    String[] numbers;
    for (String line : eachLineList) {
        numbers = line.split("\\s+");
        if (numbers.length <= 1) {
            //means if there was one or less integer each line don't do anything
            break;
        } else {
            int intNum;
            int temp = 0;
            for (String num : numbers) {
                intNum = Integer.parseInt(num);
                temp += intNum;
            }
            System.out.println(temp);
        }
    }
}}

if you enter something like this "8347 394730 3487" in each line the sum will be return back to you ~

Upvotes: 3

Piyush Vishwakarma
Piyush Vishwakarma

Reputation: 1826

I managed to get the answer somehow, with a little help from stackoverflow ofcourse

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String b=br.readLine();     
    String[] numbers=b.split(" ");
    long[] x=new long[numbers.length];
    for(int i=0;i<numbers.length;i++)
    {
        x[i]=Long.parseLong(numbers[i]);
        System.out.println(x[i]);
    }

Upvotes: 0

user3437460
user3437460

Reputation: 17454

You may want to try receiving it as a String.

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String b=br.readLine();                      
String[] token = b.split(" ");      //Split into String array by space
int[] num = new int[token.length];  //Create int array

for(int x=0; x<token.length; x++)
    num[x] = Integer.parseInt(token[x]);  //Store all string array into int array

for(int x=0; x<num.length; x++)           //Printing
    System.out.print(num[x] + " ");

Given your input in one line with spaces, the output is as follows:

Output:8347 394730 3487

Upvotes: 1

Prashant
Prashant

Reputation: 2614

YOu can check below code once.

public static void main(String[] args) throws IOException
{

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String []b=br.readLine().split(" ");
    for(int i=0;i<b.length;i++)
    {
        System.out.print(Integer.parseInt(b[i]));
    }
}

Upvotes: 0

Related Questions