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
5 Réponses
+ 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".
+ 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
+ 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);
+ 1
Thank you it worked Duncan .
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.