1. Overview
In this short tutorial, we’ll show different ways to convert from String to Integer in Groovy.
2. Casting with as
The first method that we can use for the conversion is the as keyword, which is the same as calling the class’s asType() method:
def "givenString_whenUsingAsInteger_thenConvertToInteger"() {
given:
def invalidString = "123a"
Integer integerNum = STRING_NUM as Integer
when:
def intNum = invalidString?.isInteger() ? invalidString as Integer : null
then:
intNum == null
integerNum == EXPECTED_INT
}
Like the above, we can use as int:
def "givenString_whenUsingAsInt_thenConvertToInt"() {
given:
int intNum = STRING_NUM as int
expect:
intNum == EXPECTED_INT
}
3. toInteger
Another method is from the Groovy JDK extension for java.lang.CharSequence:
def "givenString_whenUsingToInteger_thenConvertToInteger"() {
given:
int intNum = STRING_NUM.toInteger()
expect:
intNum == EXPECTED_INT
}
4. Integer#parseInt
A third way is to use Java’s static method Integer.parseInt():
def "givenString_whenUsingParseInt_thenConvertToInteger"() {
given:
int intNum = Integer.parseInt(STRING_NUM)
expect:
intNum == EXPECTED_INT
}
5. Integer#intValue
An alternative method is to create a new Integer object and call its intValue method:
def "givenString_whenUsingIntValue_thenConvertToInteger"() {
given:
int intNum = Integer.valueOf(STRING_NUM).intValue()
expect:
intNum == EXPECTED_INT
}
Or, in this case, we can also use just new Integer(stringNum):
def "givenString_whenUsingNewInteger_thenConvertToInteger"() {
given:
Integer intNum = Integer.valueOf(STRING_NUM)
expect:
intNum == EXPECTED_INT
}
6. Integer#valueOf
Similar to Integer.parseInt(), we can also use Java’s static method Integer#valueOf:
def "givenString_whenUsingValueOf_thenConvertToInteger"() {
given:
int intNum = Integer.valueOf(STRING_NUM)
expect:
intNum == EXPECTED_INT
}
7. DecimalFormat
And for our last method, we can apply Java’s DecimalFormat class:
def "givenString_whenUsingDecimalFormat_thenConvertToInteger"() {
given:
DecimalFormat decimalFormat = new DecimalFormat("#")
when:
int intNum = decimalFormat.parse(STRING_NUM).intValue()
then:
intNum == EXPECTED_INT
}
8. Exception Handling
So, if conversion fails, like if there are non-numeric characters, a NumberFormatException will be thrown. Additionally, in the case where String is null, NullPointerException will be thrown:
def "givenInvalidString_whenUsingAs_thenThrowNumberFormatException"() {
given:
def invalidString = "123a"
when:
invalidString as Integer
then:
thrown(NumberFormatException)
}
def "givenNullString_whenUsingToInteger_thenThrowNullPointerException"() {
given:
def invalidString = null
when:
invalidString.toInteger()
then:
thrown(NullPointerException)
}
To prevent this from happening, we can use the isInteger method:
def "givenString_whenUsingIsInteger_thenCheckIfCorrectValue"() {
given:
def invalidString = "123a"
def validString = "123"
when:
def invalidNum = invalidString?.isInteger() ? invalidString as Integer : false
def correctNum = validString?.isInteger() ? validString as Integer : false
then:
!invalidNum
correctNum == 123
}
9. Summary
In this short article, we’ve shown some effective ways to switch from String to Integer objects in Groovy.
When it comes to choosing the best method to convert an object type, all of the above are equally good. The most important thing is to avoid errors by first checking if the value of the String in our application can be non-numeric, empty, or null.
As usual, all code examples can be found over on GitHub.