By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

Java's NumberFormatException

P: 1
Guys this a java program in which i've encountered Java's NUMBERFORMATEXCEPTION error can you please help me out with it.......thanks in advance :)
Question: Find Kaprekar number between 1-5000.
A kaprekar number example:
Example 1:
9 = 81(square of entered number) = 8+1(sum of digits of square)=9(original number)

Example 2:
45 = 2025(square of entered number) = 20+25(sum of digits of square)=45(original number)

Example 3:
297 = 88209(square of entered number) = 88+209(sum of digits of square)=297(original number)


[import java.io.*;
public class Kaprekar
{
BufferedReader inp=new BufferedReader(new InputStreamReader(System.in));

int number,sqr,sum,count;
int lim1,lim2;
int len1,len2,len3;
String str1;
String sum1,sum2;

void enter_limits()throws IOException
{
System.out.print("Enter the 1st limit:");
lim1=Integer.parseInt(inp.readLine());
System.out.print("Enter the 2nd limit:");
lim2=Integer.parseInt(inp.readLine());
System.out.println("Limit is from "+lim1+" -- "+lim2);
}

void calc()
{
for(int i=lim1;i<=lim2;i++)
{
count=0;
sqr=i*i;
str1=Integer.toString(sqr);
len1=str1.length();

if(i%2==0)
{
len2=len1/2;
sum1=str1.substring(0,len2);
sum2=str1.substring(len2,len1);
int s1=Integer.parseInt(sum1);//NumberFormatxception
int s2=Integer.parseInt(sum2);//NumberFormatxception
sum=s1+s2;
if(sum==i)
{
System.out.print(i+" is a Kaprekar number.");
count++;
}
}
else if(i%2!=0&&i>0)
{
len3=len1-1;
len2=len3/2;
sum1=str1.substring(0,len2);
sum2=str1.substring(len2,len1);
int s1=Integer.parseInt(sum1);//NumberFormatxception
int s2=Integer.parseInt(sum2);//NumberFormatxception
sum=s1+s2;
if(sum==i)
{
System.out.print(i+" is a Kaprekar number.");
count++;
}
}
}
}

public static void main()throws IOException
{
Kaprekar k=new Kaprekar();
k.enter_limits();
k.calc();
}
}]

Output:

java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.valueOf(Integer.java:553)
at Kaprekar.calc(Kaprekar.java:50)
at Kaprekar.main(Kaprekar.java:66)
Feb 19 '12 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
When you split a single digit number you end up with one number and the empty string. For your purposes an empty string should be interpreted as zero so add

Expand|Select|Wrap|Line Numbers
  1. if(sum1.equals("")) {
  2.             sum1 = "0";
  3.         }
  4.         if(sum2.equals("")) {
  5.             sum2 = "0";
  6.         }
Also, your algorithm will probably miss some numbers because you are not searching through all possible substrings.
Feb 20 '12 #2

10K+
P: 13,264
Actually you may find it easier to treat the one digit cases as special cases so they don't mess up with your algorithm structure that much:
Expand|Select|Wrap|Line Numbers
  1. public class Kaprekar {
  2.     public static void kaprekar() {
  3.     int lim1 = 1;
  4.     int lim2 = 1000;
  5.     for (int i = lim1; i <= lim2; i++) {
  6.         if (i == 1) {
  7.         System.out.println(i + " is a Kaprekar number.");
  8.         }
  9.         int ilength = Integer.toString(i).length();
  10.         if (ilength == 1) {
  11.         continue;
  12.         }
  13.         int sqr = i * i;
  14.         String str1 = Integer.toString(sqr);
  15.         int len1 = str1.length();
  16.         String sum1 = str1.substring(0, len1 - ilength);
  17.         String sum2 = str1.substring(len1 - ilength, len1);
  18.         int s1 = Integer.parseInt(sum1);
  19.         int s2 = Integer.parseInt(sum2);
  20.         int sum = s1 + s2;
  21.         if (sum == i) {
  22.         System.out.println(i + " is a Kaprekar number.");
  23.         }
  24.     }
  25.     }
  26.  
  27.     public static void main(String[] args) {
  28.     kaprekar();
  29.     }
  30. }
  31.  
  32.  
  33.  
Feb 20 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.