0

Not able to find the error.

Find kaprekar in range. I/P: 1 100 O/P: 1 Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at kaprekar_number_range.main(kaprekar_number_range CODE: https://code.sololearn.com/cWzpz41R5w5N

11th May 2018, 10:19 AM
Raunak Jalan
Raunak Jalan - avatar
5 ответов
+ 2
Yes, but depending on the number you input, the value of left can be blank. You cannot convert blank to an integer hence the exception is thrown. If the first value you entered is too low, the squared result is only 1 digit. When you divide this by 2 to get 'l', the result is 0 so in the following line your substring doesn't find anything. left=s.substring(0,l) I entered 2 and 4 and got the exception thrown. I entered 10 and 20 and just got "no output".
11th May 2018, 11:52 AM
Duncan
Duncan - avatar
+ 2
Extra details as can't use Paste on phone when editing post. sq=(long)i*i; s=Long.toString(sq); l=(s.length())/2; Eg 1st value = 2, then sq=2*2 which is 4 s therefore = 4 l = the length of 4 which is 1, divided by 2 is 0
11th May 2018, 11:58 AM
Duncan
Duncan - avatar
+ 1
The first two values for "left" are empty. Put this in to debug after line 31: System.out.println("left: " + left); System.out.println("right: " + right); And comment out these lines // d1=Integer.parseInt(left); // d2=Integer.parseInt(right);
11th May 2018, 10:46 AM
Duncan
Duncan - avatar
+ 1
Thank you it worked Duncan .
11th May 2018, 3:34 PM
Raunak Jalan
Raunak Jalan - avatar
0
but i have to add left and right value so i have to convert these into integers that is why i wrote this line.
11th May 2018, 11:06 AM
Raunak Jalan
Raunak Jalan - avatar