Grails Cookbook - A collection of tutorials and examples

Conversion Of Java String To Integer

Assuming that we are familiar in the process of converting Java String to Double, String to Float, and String to Long. We will now be discussing the conversion of Java String To Java Integer. There are 4 ways to achieve the converted value, notice that these options we're also utilized in the previous posts related to converting Java String to a different data type - which we will be using for this post as well.

Using Integer.valueOf()
A static method that returns an Integer object which holds the value of the stated String is what we call the The Integer.valueOf().

Take Note Of The Syntax

public static Integer valueOf(String s) throws NumberFormatException

Once again, like in our previous posts regarding the conversion of String to another data type, this syntax is also present. This syntax tells us that when the parameter is an invalid integer, expect that the method will give us a NumberFormatException.
<p><strong>Understanding The Example</strong> </p>
String numAsString = "9876";
int theNumber = Integer.valueOf(numAsString);
System.out.println("The result is: " + theNumber);
Check out the output of the code below:
The result is: 9876
Remember that even if this is the one of the most common and popular option to use when converting a String to and Integer, understand that the output is not a primitive integer value and that it is an instance of the Integer class.

Using DecimalFormat
To change a number to it's String representation, we can use the java.text.DecimalFormat class. Know that this class
can also be utilized the other way around which parses into it's numerical representation.

Understanding The Example

String numAsString = "9876";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   int theNumber = decimalFormat.parse(numAsString).intValue();
   System.out.println("The number is: " + theNumber);
} catch (ParseException e) {
   System.out.println(numAsString + " is an invalid number.");
}
Check out the output below:
The number is: 9876
If there is a conflict with the indicated String, expect a ParseException from the method parse().

Special Radix
Although there are instances where using another base to get the converted Integer value is one solution, understand that we are using the base (radix) 10 in all of our examples including the following example after this one. Take note that Integer.valueOf() and Integer.parseInt() (which we will be discussed later) has the capability to receive a custom radix when converting a specific data type to another.

Understanding The Example

Hexadecimal

String numAsString = "ff";
int Num1 = Integer.valueOf(numAsString, 16);
int Num2 = Integer.parseInt(numAsString, 16);
System.out.println(Num1);
System.out.println(Num2);
Check out the result below:
255
255

Octal

String numAsString = "377";
int Num1 = Integer.valueOf(numAsString, 8);
int Num2 = Integer.parseInt(numAsString, 8);
System.out.println(Num1);
System.out.println(Num2);
Check out the result below:
255
255

Binary

String numAsString = "11111111";
int Num1 = Integer.valueOf(numAsString, 2);
int Num2 = Integer.parseInt(numAsString, 2);
System.out.println(Num1);
System.out.println(Num2);
Check out the result below:
255
255


Using Integer.parseInt()
The Integer.parseInt() static method parses the string argument as a signed decimal integer and returns an int value.

Take Note Of The Syntax

public static int parseInt(String s) throws NumberFormatException

Take a look at the parameter s, this will change to a primitive integer value. Also, this syntax tells us that when the parameter is an invalid integer, expect that the method will give us a NumberFormatException.

Understanding The Example

String numAsString = "9876";
int theNumber = Integer.parseInt(numAsString);
System.out.println("The result is: " + theNumber);
Check out the output of the code below:
The result is: 9876
While the result we got in the example above using the Integer.valueOf() is an instance of the of the Integer class, the result in this case differs. Because the output value we got is the opposite of what we mentioned a while ago - it's just a plain primitive integer value.

Using Integer().intValue()
Our last option will be invoking intValue() method, so that we can make an instance of the Integer class.

Understanding The Example

String numAsString = "9876";
Integer intObject = new Integer(numAsString);
int theNumber = intObject.intValue();

We can shorten to:

String numAsString = "9876";
int theNumber = new Integer(numAsString).intValue();

or just:

int theNumber = new Integer("9876").intValue();