455,806 Members | 1,380 Online
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