August 21, 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 alvin
 *
 */
public class StringToNumber {

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

/* 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)


I got java.lang.NumberFormatException while using
Long.parseLong(valueInString)
and solved it by using the
BigDecimal valueInDecimal = new BigDecimal(valueInString);


This book helps you to know the concepts of java, Hurry Buy one copy




Tik Toc Toe in python, two player game, playing with computer complete source code available soon...

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments