2 Replies Latest reply: Apr 22, 2008 10:28 PM by 807591 RSS

    java.lang.Numberformatexception

    807591
      hi i was trying to make a class like BigInteger
      class Huge
      {
         public String value;
         public Huge(String val)
         {
           value = val;
         }
      
           public void display()
           {
              System.out.println(value);
           }
      
           public Huge add(Huge val)
           {
      
              Huge result = new Huge ("000000000000000000000000000000");
              int neww = 0, prev = 0, j = result.value.length() -1;
              int placeVal = 0;
              int leadingZero = value.length() - val.value.length();
              for (int i = 0; i < leadingZero; i++)
              {
                 val.value = '0' + val.value;
              }
              
           for(int i = value.length() - 1; i > - 1; i--)
           {
              neww = (Integer.parseInt("" + value.charAt(i)) + Integer.parseInt("" + val.value.charAt(i)) + neww)/10;
              placeVal = Integer.parseInt("" + value.charAt(i)) + Integer.parseInt("" + val.value.charAt(i)) + prev - neww * 10;
                  prev = neww;
              result.value = result.value.substring(0,j) + (char)((int) '0' + placeVal) + result.value.substring(j+1);
                  j--;
              }
              if (neww > 0) result.value = result.value.substring(0,j) + (char)((int) '0' + neww) + result.value.substring(j+1);
                  return result;
         }
      }
      public class HugeProgram
      {
         public static void main (String[] args)
         {
           Huge firstNumber = new Huge ("1234567221234123489011231");
           System.out.print("The first number is: ");
           firstNumber.display();
      
           Huge secondNumber = new Huge ("901928331234123437491123");
           System.out.print("The second number is: ");
           secondNumber.display();
      
           Huge result = firstNumber.add (secondNumber);
      
           System.out.print("The summation of the two numbers are: ");
           System.out.println (Integer.parseInt(result.value));
           
      }
      }
      the program works i guess.. but what comes out is

      C:\Documents and Settings\Compaq_Owner\Desktop>java HugeProgram
      The first number is: 1234567221234123489011231
      The second number is: 901928331234123437491123
      The summation of the two numbers are: Exception in thread "main" java.lang.Numbe
      rFormatException: For input string: "000002136495552468246926502354"
      at java.lang.NumberFormatException.forInputString(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at HugeProgram.main(HugeProgram.java:20)


      so i have to fix the parseint somehow? thank you
        • 1. Re: java.lang.Numberformatexception
          807591
          You're trying to parse a String representation of an integer with arbitrary size to an int (with fixed maximum size). Integer.parseInt() tries to parse an int from the String you give it. Seeing as the String represents a number way bigger than can be represented by an int, it throws an error correctly.

          The real WTF lies in you parsing a String into an int so that it can be sent to an OutputStream and translated back into a String representation. Forget the Integer.parseInt call completely and it'll work fine.
          • 2. Re: java.lang.Numberformatexception
            807591
            thanks a lot : )