January 01, 2019

How to handle NumberFormatException: For input string: "0.0"

How to handle NumberFormatException: For input string: "0.0"


Here is the solution for that problem ( Use BigDecimal instead of Long.parseLong())

/**
 * Exception in thread "main" java.lang.NumberFormatException: For input string: "0.0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at com.konzern.solution.StringToNumber.main(StringToNumber.java:18)
 */
package com.konzern.solution;

import java.math.BigDecimal;

/**
 * @author cfed
 *
 */
public class StringToNumber {

/**
* @param
*/
public static void main(String[] args) {

/* String valueInString = "0.0";
long valueInLong = Long.parseLong(valueInString);
System.out.println(valueInLong);*/

String valueInString = "0.0";
BigDecimal valueInDecimal = new BigDecimal(valueInString);
long valueInLong = valueInDecimal.longValue();
System.out.println(valueInLong);

}

}


Exception in thread "main" java.lang.NumberFormatException: For input string: "0.0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at com.konzern.solution.StringToNumber.main(StringToNumber.java:18)





No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments