How to fix?
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.
CodePudding user response:
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;
}
}
CodePudding user response:
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.
What are you trying to do in that try-catch block?
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;
}
CodePudding user response:
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();