In this post, we will explore different ways to parse a string as a signed decimal integer in Java, using both built-in methods and external libraries.

Parsing a string as a signed decimal integer means if the value of the string is negative, the output should preserve the sign in the integer. This string to integer conversion is often useful in Java, like to perform arithmetic operations on a string that contains a number, or compare two strings that represent numbers.

1. Using Integer class

The Integer class provides many methods to parse a string as a signed decimal integer. These methods are static and don’t need instantiation before being called and throw a NumberFormatException if the string does not contain a valid integer.

1. Using parseInt() method

One of the simplest and most widely used ways to parse a string as a signed decimal integer in Java is to use the Integer.parseInt() method. This method takes a string as a parameter and returns an int primitive value. For example:

 
The advantage of this method is that it is very fast and concise. It does not create any new objects or copy any data. It simply converts the string to an int value.

2. Using valueOf() or decode() method

Another way to parse a string as a signed decimal integer in Java is to use the Integer.valueOf() method. This method takes a string as a parameter and returns an Integer object. For example:

 
Note that it creates a new Integer object instead of returning a primitive value. This may consume more memory and affect performance compared to the Integer.parseInt() method. To get the return value as a primitive int, we can call the intValue() method or do implicit unboxing:

2. Using Scanner.nextInt() method

Another option is to use the Scanner class, which can parse strings by breaking them into tokens. We can use the nextInt() method to get the only token of the input string as an int.

 
The above approach will fail for strings like "123 34" as Scanner breaks its input into tokens using a delimiter pattern, which is whitespace by default. We can use the useDelimiter() method to specify a delimiter that reads the complete string as one single token. Now, the nextInt() method will throw an InputMismatchException if the next token is not a valid integer.

3. Using Guava Library

Another way to parse a string as a signed decimal integer in Java is using the Ints.tryParse() method of the Guava library. This method parses the string argument as a signed decimal integer value. It takes a string as a parameter and returns an Integer object. For example:

 
The advantage of this method is that it is more concise and powerful than the built-in methods. Unlike Integer class static methods, it returns null instead of throwing an exception if the string cannot be parsed as an integer value. However, this approach requires an external dependency on Guava. The method creates a new object instead of returning a primitive value, which may consume more memory than Integer.parseInt() method.

4. Using Apache Commons Lang

Apache Commons Lang library provides a better way to parse a string as a signed decimal integer in Java. The NumberUtils class has the toInt() method, which takes a string as a parameter and returns a primitive int value. For example:

 
Like Guava, this method does not throw an exception if the string does not contain a parsable integer. However, it returns 0 value instead of null. So, it is recommended to define our own default value in the optional second argument of this method.

That’s all about parsing a string as a signed decimal integer in Java.

 
Related Post:

Convert an Integer to a String in Java