Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is NumberUtils.isParsable in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

isParsable() is a staticthe methods in Java that can be called without creating an object of the class. method of the NumberUtils class that is used to check if the given string is parsable to a number or not.

Hexadecimal and scientific notations are not considered parsable.

How to import NumberUtils

The definition of NumberUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the NumberUtils class as follows:

import org.apache.commons.lang3.math.NumberUtils;

Syntax

public static boolean isParsable(final String str)

Parameters

  • final String str: The string to check.

Return value

This method returns true if the string is parsable. Otherwise, it returns false.

Code

import org.apache.commons.lang3.math.NumberUtils;
public class Main{
public static void main(String[] args){
// Example 1
String valueToConvert = "234.323232232";
System.out.printf("NumberUtils.isParsable(%s) = %s", valueToConvert, NumberUtils.isParsable(valueToConvert));
System.out.println();
// Example 2
valueToConvert = "0xABED";
System.out.printf("NumberUtils.isParsable(%s) = %s", valueToConvert, NumberUtils.isParsable(valueToConvert));
System.out.println();
}
}

Output

The output of the code will be as follows:

NumberUtils.isParsable(234.323232232) = true
NumberUtils.isParsable(0xABED) = false

Example 1

  • string = 234.323232232

The method returns true because the string is parsable.

Example 2

  • string = 0xABED

The method returns false because the string is a hexadecimal value and hexadecimal values are not supported.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring