0

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("");
    }
}
}

1 Answers1

0

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("");
    }
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
  • yes, I am taking a single integer first then space separated number's string. but I am already using br.readline for t (single number) and s.nextToken for all other inputs (rest of the inputs are space saperated numbers). Also,I have edited my question with input I want to take. – sahil mehta Dec 29 '17 at 08:33
  • I want to read t through readline method and rest through nextToken method. still I tried your suggestion but that didn't work. I am getting this now: Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at A.main(SMRSTRwa.java:24) – sahil mehta Dec 29 '17 at 08:49
  • I edited my answer, program is now running all the way through – Bentaye Dec 29 '17 at 09:02
  • yeah! thanks, I got it now. one more thing, does it make any difference if we import a whole package like import java.io.*; or importing specifically what is required like import java.io.BufferedReader; ? – sahil mehta Dec 29 '17 at 09:21
  • Importing a single class is more efficient, however I don't think it makes a big difference. I don't usually bother with that. – Bentaye Dec 29 '17 at 09:30
  • ok, thanks. I don't bother too but I have seen many codes importing specific classes. So just asked. – sahil mehta Dec 29 '17 at 09:56
  • This Thread here `https://stackoverflow.com/questions/7128348/performance-difference-between-a-wild-card-import-and-the-required-class-import` says that there is no difference when you run your program. The only difference is at compile time and it is a very small difference. So just use what is clearer in your code, for readability. – Bentaye Dec 29 '17 at 10:00