-1

So i was solving a question in competitive programming where i had to take these numbers as input...

3 
40 40 100
45 45 90
180 1 1

and here's my code:

package CP;
import java.io.*;
import java.util.*;
class Test {
    public static void main(String[] args)throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int t=Integer.parseInt(br.readLine());
        while(t-->0){
            StringTokenizer s=new StringTokenizer(br.readLine());
            int a=Integer.parseInt(s.nextToken());
            int b=Integer.parseInt(s.nextToken());
            int c=Integer.parseInt(s.nextToken());
            if(a+b+c==180) System.out.println("YES");
            else System.out.println("NO");
        }
        br.close();
    }
}

so this code works fine when i take inputs line by line. but when i take all the lines as input at a time it shows [Exception in thread "main" java.lang.NumberFormatException: For input string: "3"] in line 7. why is this happening?

  • 3
    Looks like there is an not printable char at the end of the string. BTW Take care of java naming conventions. package names should be lower case – Jens Jan 11 '22 at 14:51
  • 1
    The question is: what is your input? If your input is from a file, inspect its exact contents using a Hex Editor (like HxD) or some other tool. It may also be related to a Byte Order Mark. – MC Emperor Jan 11 '22 at 14:58
  • uhh this is not working in an IDE but when it got accepted when i submitted this code...idk why – manofculture Jan 11 '22 at 15:06

1 Answers1

1

There is a space after the 3 in the input. This causes the Integer.parseInt(String) to fail. Trimming the line of input should suffice to solve your issue.

int t = Integer.parseInt(br.readLine().trim());
vsfDawg
  • 1,425
  • 1
  • 9
  • 12