Reputation: 4291
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.
Upvotes: 0
Views: 253
Reputation: 3765
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
Upvotes: 1
Reputation: 11
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;
}
Upvotes: 1
Reputation: 23
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();
Upvotes: 1
Reputation: 149
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;
}
}
Upvotes: 0