Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Convert String to Integer in Groovy

$
0
0

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:

@Test
void givenString_whenUsingAsInteger_thenConvertToInteger() {
    def stringNum = "123"
    Integer expectedInteger = 123
    Integer integerNum = stringNum as Integer

    assertEquals(integerNum, expectedInteger)
}

Like the above, we can use as int:

@Test
void givenString_whenUsingAsInt_thenConvertToInt() {
    def stringNum = "123"
    int expectedInt = 123
    int intNum = stringNum as int

    assertEquals(intNum, expectedInt)
}

3. toInteger

Another method is from the java.lang package:

@Test
void givenString_whenUsingToInteger_thenConvertToInteger() {
    def stringNum = "123"
    int expectedInt = 123
    int intNum = stringNum.toInteger()

    assertEquals(intNum, expectedInt)
}

4. Integer#parseInt

A third way is to use Java's static method Integer.parseInt():

@Test
void givenString_whenUsingParseInt_thenConvertToInteger() {
    def stringNum = "123"
    int expectedInt = 123
    int intNum = Integer.parseInt(stringNum)

    assertEquals(intNum, expectedInt)
}

5. Integer#intValue

An alternative method is to create a new Integer object and call its intValue method:

@Test
void givenString_whenUsingIntValue_thenConvertToInteger() {
    def stringNum = "123"
    int expectedInt = 123
    int intNum = new Integer(stringNum).intValue()

    assertEquals(intNum, expectedInt)
}

Or, in this case, we can also use just new Integer(stringNum):

@Test
void givenString_whenUsingNewInteger_thenConvertToInteger() {
    def stringNum = "123"
    int expectedInt = 123
    int intNum = new Integer(stringNum)

    assertEquals(intNum, expectedInt)
}

6. Integer#valueOf

Similar to Integer.parseInt(), we can also use Java's static method Integer#valueOf:

@Test
void givenString_whenUsingValueOf_thenConvertToInteger() {
    def stringNum = "123"
    int expectedInt = 123
    int intNum = Integer.valueOf(stringNum)

    assertEquals(intNum, expectedInt)
}

7. DecimalFormat

And for our last method, we can apply Java's DecimalFormat class:

@Test
void givenString_whenUsingDecimalFormat_thenConvertToInteger() {
    def stringNum = "123"
    int expectedInt = 123
    DecimalFormat decimalFormat = new DecimalFormat("#")
    int intNum = decimalFormat.parse(stringNum).intValue()

    assertEquals(intNum, expectedInt)
}

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:

@Test(expected = NumberFormatException.class)
void givenInvalidString_whenUsingAs_thenThrowNumberFormatException() {
    def invalidString = "123a"
    invalidString as Integer
}

@Test(expected = NullPointerException.class)
void givenNullString_whenUsingToInteger_thenThrowNullPointerException() {
    def invalidString = null
    invalidString.toInteger()
}

To prevent this from happening, we can use the isInteger method:

@Test
void givenString_whenUsingIsInteger_thenCheckIfCorrectValue() {
    def invalidString = "123a"
    def validString = "123"
    def invalidNum = invalidString?.isInteger() ? invalidString as Integer : false
    def correctNum = validString?.isInteger() ? validString as Integer : false

    assertEquals(false, invalidNum)
    assertEquals(123, correctNum)
}

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.


Viewing all articles
Browse latest Browse all 4535

Trending Articles