Java Number Classes Java
Following examples shows how to parse Strings to numeric primitives (byte, short, int, long, float, double).
Examples
Using parse methods e.g. Integer#parseInt
Following are the parse methods defined in Number wrapper classes.
java.lang.Byte
public static byte parseByte(String s) throws NumberFormatException
java.lang.Short
public static short parseShort(String s) throws NumberFormatException
java.lang.Integer
public static int parseInt(String s) throws NumberFormatException
java.lang.Long
public static long parseLong(String s) throws NumberFormatException
java.lang.Float
public static float parseFloat(String s) throws NumberFormatException
java.lang.Double
public static double parseDouble(String s) throws NumberFormatException
Following example parse a string to int
package com.logicbig.example;
public class Example1 {
public static void main(String[] args) {
int i = Integer.parseInt("54");
System.out.println(i);
}
}
54
Usually it's not safe to pass a dynamic string to one of the above parse methods. For example:
package com.logicbig.example;
public class Example2 {
public static void main(String[] args) {
String intString = "five";
int i = Integer.parseInt(intString);
System.out.println(i);
}
}
ava.lang.NumberFormatException: For input string: "five" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at com.logicbig.example.Example2.main(Example2.java:5)
Safe ways to parse strings to numbers
By catching java.lang.NumberFormatException
package com.logicbig.example;
public class Example3 {
public static void main(String[] args) {
int i = parseStringToInt("five", -1);
System.out.println(i);
}
private static int parseStringToInt(String input, int defaultValue){
try {
return Integer.parseInt(input);
} catch (NumberFormatException e) {
return defaultValue;
}
}
}
-1
By checking input before parsing
Following examples check the input string for being integer with regex pattern:
package com.logicbig.example;
public class Example4 {
public static void main(String[] args) {
int i = parseToInt("5", -1);
System.out.println(i);
i = parseToInt("05", -1);
System.out.println(i);
i = parseToInt("000", -1);
System.out.println(i);
i = parseToInt("-5", -1);
System.out.println(i);
i = parseToInt("+5", -1);
System.out.println(i);
i = parseToInt("-", -1);
System.out.println(i);
i = parseToInt("five", -1);
System.out.println(i);
}
private static int parseToInt(String input, int defaultInt) {
System.out.printf("Input: '%s'%n", input);
if (input == null) {
return defaultInt;
} else if (input.matches("[-+]?\\d+")) {
return Integer.parseInt(input);
} else
return defaultInt;
}
}
Input: '5' 5 Input: '05' 5 Input: '000' 0 Input: '-5' -5 Input: '+5' 5 Input: '-' -1 Input: 'five' -1
It works but it's very difficult to create a perfect regex for number checking e.g. above example doesn't take care of integer range,
so any input which is out of the int range, will still throw NumberFormatException.
For floating point checking we can use [+-]?([0-9]*[.])?[0-9]+
By using Apache lang NumberUtils
pom.xml<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
package com.logicbig.example;
import org.apache.commons.lang3.math.NumberUtils;
public class Example5 {
public static void main(String[] args) {
int i = NumberUtils.toInt("5", 0);
System.out.println(i);
i = NumberUtils.toInt("-5", 0);
System.out.println(i);
i = NumberUtils.toInt("+5", 0);
System.out.println(i);
i = NumberUtils.toInt("05", 0);
System.out.println(i);
i = NumberUtils.toInt("five", 0);
System.out.println(i);
}
}
5 -5 5 5 0
NumberUtils provide following methods for parsing
public static byte toByte(final String str, final byte defaultValue)
public static short toShort(final String str, final short defaultValue)
public static int toInt(final String str, final int defaultValue)
public static long toLong(final String str, final long defaultValue)
public static float toFloat(final String str, final float defaultValue)
public static double toDouble(final String str, final double defaultValue)
All above methods internally catch NumberFormatException. e.g.
public static double toDouble(final String str, final double defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Double.parseDouble(str);
} catch (final NumberFormatException nfe) {
return defaultValue;
}
}
|