Java's NumberFormatException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anand Francis
    New Member
    • Feb 2012
    • 1

    #1

    Java's NumberFormatException

    Guys this a java program in which i've encountered Java's NUMBERFORMATEXC EPTION 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(origi nal number)

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

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


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

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

    void enter_limits()t hrows IOException
    {
    System.out.prin t("Enter the 1st limit:");
    lim1=Integer.pa rseInt(inp.read Line());
    System.out.prin t("Enter the 2nd limit:");
    lim2=Integer.pa rseInt(inp.read Line());
    System.out.prin tln("Limit is from "+lim1+" -- "+lim2);
    }

    void calc()
    {
    for(int i=lim1;i<=lim2; i++)
    {
    count=0;
    sqr=i*i;
    str1=Integer.to String(sqr);
    len1=str1.lengt h();

    if(i%2==0)
    {
    len2=len1/2;
    sum1=str1.subst ring(0,len2);
    sum2=str1.subst ring(len2,len1) ;
    int s1=Integer.pars eInt(sum1);//NumberFormatxce ption
    int s2=Integer.pars eInt(sum2);//NumberFormatxce ption
    sum=s1+s2;
    if(sum==i)
    {
    System.out.prin t(i+" is a Kaprekar number.");
    count++;
    }
    }
    else if(i%2!=0&&i>0)
    {
    len3=len1-1;
    len2=len3/2;
    sum1=str1.subst ring(0,len2);
    sum2=str1.subst ring(len2,len1) ;
    int s1=Integer.pars eInt(sum1);//NumberFormatxce ption
    int s2=Integer.pars eInt(sum2);//NumberFormatxce ption
    sum=s1+s2;
    if(sum==i)
    {
    System.out.prin t(i+" is a Kaprekar number.");
    count++;
    }
    }
    }
    }

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

    Output:

    java.lang.Numbe rFormatExceptio n: For input string: ""
    at java.lang.Numbe rFormatExceptio n.forInputStrin g(NumberFormatE xception.java:4 8)
    at java.lang.Integ er.parseInt(Int eger.java:468)
    at java.lang.Integ er.valueOf(Inte ger.java:553)
    at Kaprekar.calc(K aprekar.java:50 )
    at Kaprekar.main(K aprekar.java:66 )
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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

    [CODE=java]if(sum1.equals( "")) {
    sum1 = "0";
    }
    if(sum2.equals( "")) {
    sum2 = "0";
    }[/CODE]

    Also, your algorithm will probably miss some numbers because you are not searching through all possible substrings.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      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:
      [CODE=java]
      public class Kaprekar {
      public static void kaprekar() {
      int lim1 = 1;
      int lim2 = 1000;
      for (int i = lim1; i <= lim2; i++) {
      if (i == 1) {
      System.out.prin tln(i + " is a Kaprekar number.");
      }
      int ilength = Integer.toStrin g(i).length();
      if (ilength == 1) {
      continue;
      }
      int sqr = i * i;
      String str1 = Integer.toStrin g(sqr);
      int len1 = str1.length();
      String sum1 = str1.substring( 0, len1 - ilength);
      String sum2 = str1.substring( len1 - ilength, len1);
      int s1 = Integer.parseIn t(sum1);
      int s2 = Integer.parseIn t(sum2);
      int sum = s1 + s2;
      if (sum == i) {
      System.out.prin tln(i + " is a Kaprekar number.");
      }
      }
      }

      public static void main(String[] args) {
      kaprekar();
      }
      }


      [/CODE]

      Comment

      Working...