Java Runtime error for using Integer.parseInt() , though proper updation of integer in loop

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;
import java.util.Scanner;

/* Name of the class has to be “Main” only if the class is public. */
class Main
{
public static void main (String[] args) throws NumberFormatException,IOException
{
// your code goes here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=0,r=0;
String s=br.readLine();
t=Integer.parseInt(s);
while(t>0)
{
String t1=br.readLine();
int c=0,m=0,d=0;
r=Integer.parseInt(t1);
while(r>0)
{
String temp=br.readLine();
String temp2=br.readLine();
int p1=Integer.parseInt(temp);
int p2=Integer.parseInt(temp2);
if(p1>9)
p1=digitCount(p1);
if(p2>9)
p2=digitCount(p2);
if(p1>p2)
c++;
else if(p1<p2)
m++;
else
d++;
–r;
}
if(c>m)
System.out.print(0+" “+c);
else if(c<m)
System.out.print(1+” “+m);
else
System.out.print(2+” "+d);
–t;
}
}
public static int digitCount(int n)
{
if(n<9)
return n;
int r=n%10+digitCount(n/10);
if(r>=10)
r=r%10+digitCount(r/10);
return r;
}
}

It shows:
Exception in thread “main” java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:542) at java.lang.Integer.parseInt(Integer.java:615) at Codechef.main(Main.java:17)

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also: what Problem are you trying to solve? :slight_smile:

https://www.codechef.com/viewsolution/36181410

This is the link of my submission. I got runtime error for using Integer.parseInt().

1 Like

Yes, that’s expected: when reading the sample input, for example, you will call parseInt() on the String:

10 4

which is not the String representation of an integer.

Other java programmers seem to use a Scanner object - perhaps you should do the same:

Scanner sc = new Scanner (System.in);
    int n = sc.nextInt ();
    // ... etc ...
1 Like

First I tried with Scanner obj but that shows java.util:(No such element Exception) even though I imported java.util.Scanner;

So only I moved on to this BufferedReader IO method…Anyother ways to overcome this runtime error…?

Link of submission using Scanner method:
https://www.codechef.com/viewsolution/36181327

Scanner should work; see e.g. this solution.

When you had the “No such element Exception”, were you trying to “Run” without Providing “Custom Input”?

Edit:

Oh, that’s just because your logic/ output is wrong; for the example test input:

2
3
10 4
8 12
7 6
2
5 10
3 4

that solution outputs:

0 22 0

That solution seems to be reading the input in correctly, as far as I can tell.

1 Like

Ok thank you :blush:

1 Like