javaexceptionstringtokenizerbufferedstream

String Tokenizer giving exception while taking space saperated values using BufferedReader


I tried to start using BufferedReader instead of Scanner. While coding for a question on codechef (SMRSTR), I tried taking space separated inputs by using StringTokenizer but it is raising exception i.e NumberFormatException. I found some question on StackOverflow regarding it but I think my problem is different, so I posted one.
Input: 1
2 3
2 3
5 100 8

I am getting:

Exception in thread "main" java.lang.NumberFormatException: For input string: "2 3"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at A.main(A.java:11)

I am getting first input t correctly from br.readLine(); But next inputs n,q are giving the mentioned exception. I think the problem is in the nextToken from StringTokenizer, but still not getting it clearly.

Here is the code:

import java.io.*;
import java.util.*;

 class A{
public static void main(String arg[]) throws IOException
{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer s = new StringTokenizer(br.readLine());

    int t= Integer.parseInt(br.readLine());
    while(t-->0)
    {
        int n,q,i;
        n=Integer.parseInt(s.nextToken());
        q=Integer.parseInt(s.nextToken());
        int D[]= new int[n];
        int Q[]=new int[q];
        long x=1;
        for(i=0;i<n;i++)
        {
            D[i]=Integer.parseInt(s.nextToken());
            x=x*D[i];
        }
        for(i=0;i<q;i++)
        {
            Q[i]=Integer.parseInt(s.nextToken());
            if(x>1000000000)
                Q[i]=0;
            else
            Q[i]=(int)(Q[i]/x);
        }
        for(i=0;i<q;i++)
            System.out.print(Q[i]+" ");
    System.out.println("");
    }
}
}

Solution

  • Assuming your first line is a single number and your second line a string of space separated numbers (if not, edit your question with your actual input)

    I think you want to read t this way:

    int t = Integer.parseInt(s.nextToken());
    

    Then read your next line into your tokenizer

    s = new StringTokenizer(br.readLine());
    

    The code before the while loop should be:

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer s = new StringTokenizer(br.readLine());
    
    int t = Integer.parseInt(s.nextToken());
    s = new StringTokenizer(br.readLine());
    

    EDIT

    You need to read each line in the tokenizer before using the next Int method. This should work.

    Input:

    1 
    2 3  
    2 3 
    5 100 8
    

    Output:

    0 16 1 
    

    Working code:

    public static void main(String arg[]) throws IOException {
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
        // read first line in tokenizer
        StringTokenizer s = new StringTokenizer(br.readLine());
        //parse t
        int t = Integer.parseInt(s.nextToken());
    
        // read second line in tokenizer
        s = new StringTokenizer(br.readLine());
    
        while(t-->0) {
            int n,q;
            // parse n and q (2, 3)
            n=Integer.parseInt(s.nextToken());
            q=Integer.parseInt(s.nextToken());
    
            int D[]= new int[n];
            int Q[]=new int[q];
            long x=1;
            // read third line in tokenizer
            s = new StringTokenizer(br.readLine());
            for(int i=0;i<n;i++) {
                D[i]=Integer.parseInt(s.nextToken());
                x=x*D[i];
            }
            // read fourth line in tokenizer
            s = new StringTokenizer(br.readLine());
            for(int i=0;i<q;i++) {
                Q[i]=Integer.parseInt(s.nextToken());
                if(x>1000000000)
                    Q[i]=0;
                else
                    Q[i]=(int)(Q[i]/x);
            }
            for(int i=0;i<q;i++)
                System.out.print(Q[i]+" ");
    
            System.out.println("");
        }
    }