0

How to fix this issue?

java.lang.NumberFormatException: at java.lang.NumberFormatException.forInputString(Unknown Source)

I am doing some example problem and my code is working fine for the first string and digit. (Commented one)

But when change the new string and digit (Current one) I am getting this error :

java.lang.NumberFormatException: For input string: "299858953917872714814599237991174513476623756395992135212546127959342974628712329595771672911914471"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at com.codejam.q1.problems.maxResult.removeDigit(maxResult.java:21)
    at com.codejam.q1.problems.maxResult.main(maxResult.java:10)

Here is my code. Anywhere I am missing something ?

public class maxResult {
    public static void main(String[] args) {
        //String str = "1231";
        String str = "2998589353917872714814599237991174513476623756395992135212546127959342974628712329595771672911914471";
        //char digit = '1';
        char digit = '3';
        System.out.println(removeDigit(str,digit));
    }
    
    public static String removeDigit(String number, char digit) {
        long result = 0;
        for(int i = 0; i<number.length(); i++) {
            char num = number.charAt(i);
            if(num == digit) {
                String myStr = number.substring(0, i) + number.substring(i + 1); 
                try{
                    long myNum = Long.parseLong(myStr);  
                    if(myNum > result) {
                        result = myNum;
                    }
                }
                catch (NumberFormatException ex){
                    ex.printStackTrace();
                }
            }
         }
        String s = String.valueOf(result);  
        return s;
    }
}

Even though I change int to long but no change in result.

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
David
  • 4,266
  • 8
  • 34
  • 69
  • If you are trying to remove every occurrence of the digit `3` from `str`, then you can simply do: `str.replaceAll("3", "");` Or did I misunderstand what you are trying to do? – Abra May 01 '22 at 03:54
  • Alternatively, consider using class [java.math.BigInteger](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigInteger.html) Refer to this SO question: [Is there an upper bound to BigInteger?](https://stackoverflow.com/questions/12693273/is-there-an-upper-bound-to-biginteger) – Abra May 01 '22 at 04:15

4 Answers4

1

The number is too long for a long. Longs go from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808.

Try doing this :

public static String removeDigit(String number, char digit) {
    double temp = 0;
    String result="";
    for(int i = 0; i<number.length(); i++) {
        char num = number.charAt(i);
        if(num == digit) {
            String myStr = number.substring(0, i) + number.substring(i + 1); 
            try{
                double myNum = Double.parseDouble(myStr);  
                if(myNum > temp) {
                    temp = myNum;
                    result=myStr;
                }
            }
            catch (NumberFormatException ex){
                ex.printStackTrace();
            }
        }
    }
    return result;
}
1

Your number is too big for a long value. The maximum long value is 9,223,372,036,854,775,807. You can use BigInteger, which essentially has no limit.

Using long

long result = 0;
// ...
long myNum = Long.parseLong(myStr);  
if(myNum > result) {
    result = myNum;
}
// ...
String s = String.valueOf(result);  
return s;

Using BigInteger

import java.math.BigInteger;
// ...
BigInteger result = BigInteger.ZERO;
// ...
BigInteger myNum = new BigInteger(myStr);
result = myNum.max(result);
// ...
return result.toString();
ghost1034
  • 23
  • 7
1

The problem you get is that you are exceeding the limit of the int and the long. Let us see the limits of some number storing types and then use the best one:

Type Size Value Exceeds
int 32 bit -2,147,483,648 to 2,147,483,647 Yes
long 64 bit -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Yes
float 32 bit 3.40282347 x 1038 to 1.40239846 x 10-45 Yes
double 64 bit 1.7976931348623157 x 10308 to 4.9406564584124654 x 10-324 Yes
BigInteger 32 bit 2^64billion No

Here, we find that BigInteger is the class we need to use. So, instead of using a long or int for it, use BigInteger. To know more about BigInteger, visit here.

Also to know how to use a big integers refer to the answer here

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
  • You evidently copied a source that used superscripted exponents but removed the superscripting, giving results which are quite wrong. Also BigInteger isn't infinite; its limit is about 2^64billion, a number which in decimal has about 19billion digits, which is _very_ big but finite. – dave_thompson_085 May 01 '22 at 06:03
  • Ok. @dave_thompson_085. I didnt know the vlue so i wrote infinite. U can edit and give the value there – Sambhav Khandelwal May 01 '22 at 09:04
0

You can use BigDecimal instead of long.

public class Application {
        public static void main(String[] args) {
            //String str = "1231";
            String str = "2998589353917872714814599237991174513476623756395992135212546127959342974628712329595771672911914471";
            //char digit = '1';
            char digit = '3';
            System.out.println(removeDigit(str,digit));
        }

        public static BigDecimal removeDigit(String number, char digit) {
            BigDecimal result = BigDecimal.ZERO;
            for(int i = 0; i<number.length(); i++) {
                char num = number.charAt(i);
                if(num == digit) {
                    String myStr = number.substring(0, i) + number.substring(i + 1);
                    try{
                        BigDecimal myNum = new BigDecimal(myStr);
                        if(myNum.compareTo(result)>0) {
                            result = myNum;
                        }
                    }
                    catch (NumberFormatException ex){
                        ex.printStackTrace();
                    }
                }
            }
            return result;
        }
    }