1

Question: you have to check for n test cases that multiplication of a and b is equal to 3rd input value or not.If equal then print YES otherwise NO. Input: 2 5 6 30 4 3 20

Output: YES NO

Explanation: 2 is number of test cases. 5 is a & 6 is b ,multiplication is 30 so YES

Constraints :

0 < test cases <= 1000000000 0 < a < b <= 10000000

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;


class ques1 
{
    public static void main(String[] args) throws Exception
    {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        Scanner sc=new Scanner(System.in);
        BigInteger test=new BigInteger(sc.next());

        while(!test.equals(BigInteger.ZERO))
        {
            Scanner s=new Scanner(br.readLine());
            s.useDelimiter(" ");
            BigInteger p=new BigInteger(s.next());
            BigInteger q=new BigInteger(s.next());
            BigInteger r=new BigInteger(s.next());
            if(p.multiply(q).equals(r))
            {
                System.out.println("YES");
            }
            else
            {
                System.out.println("NO");
            }
            test=test.subtract(BigInteger.ONE);
        }
    }
}

This program is working fine in eclipse.But while running on online compilers like ideone.com,etc it is giving error message as following:

Exception in thread "main" java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at java.util.Scanner.<init>(Scanner.java:702)
    at ques1.main(Main.java:18)

Constraints are so big so i am using biginteger.I had also used stringtokenizer instead of scanner of input separation,but it is also giving same error. i have to take input in a spacing format so i am using stringtokenizer or scanner. Is there is any other way to separate them.

2
  • 1
    br.readLine() is throwing the NPE, is there a line for it to read..? Commented Apr 20, 2015 at 17:45
  • yea, you should check br.readLine()==null, before new Scanner(). Commented Apr 20, 2015 at 17:49

1 Answer 1

1

br.readLine() will return null when reaching the end of the file. And new Scanner(String) won't accept null as parameter. Apart from that you should always check for valid input / if you can read data from a resource.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.