Java Integer parseInt() method example

Integer.parseInt in java Overview

The Integer.parseInt() java method is used primarily in parsing a String method argument into an Integer object. The Integer object is a wrapper class for the int primitive data type of java API.Eventhough the conversion of String to Integer object is a basic stuff, I am suggesting that we should have a mastery on it. In my years of java programming, I have encountered so many instances that I have to convert from String object to another data type. The Integer.parseInt method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Integer.Make a note that the parseInt method of Integer class is static thus it should be accessed statically. Mean to say the we would be calling this method in this format:

Integer.parseInt(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseInt method non statically.

Quick Background on Integer Object

  • The Integer class is already around since JDK 1.0.
  • wraps a primitive data type int
  • The Integer object is preferred than int if we are planning to serialize,
  • Integer can contain null value

Java Integer.parseInt method Syntax

There are two overloaded method for parseInt method of Java Integer class.

To give a background overloaded methods are those methods that have the same method name but with different signatures.

Method 1:

public static int parseInt(String strVal)throws NumberFormatException

Method 2:

public static int parseInt(String strVal,int radixValue)throws NumberFormatException

Discussion on Integer.parseInt overloaded method

There are two overloaded method of Integer.parseInt and they are similar in functionality. Depending on the complexity on what you want to achieve, we can use any of the two method.Let’s take a look first with the method

Integer.parseInt(String strVal)

This method just parse the String input strVal and eventually return an Integer object. As you would have noticed, the String method argument should be a number which means it should be consists by our counting numbers.To give an overview on counting numbers, those are numbers in decimal format which of base 10. In java programming terms, decimal format is radix 10. Thus it is understood that the string input should be consisting of decimal numbers only otherwise invoking the Integer.parseInt method would throw a NumberFormatException.Let’s take the following code snippet:

Integer intValue = Integer.parseInt("103");System.out.println(intValue);

The above code result will give an output of 103. As you would have noticed, we just convert the string “103” into an Integer object using the static method Integer.parseInt.You might be wondering what if we put a letter or any other characters beside numbers. The Integer method parseInt would throw NumberFormatException. Like for this example:

System.out.println(Integer.parseInt("A"));

Running the above code would result to:

Exception in thread "main" java.lang.NumberFormatException: For input string: "A"	
at java.lang.NumberFormatException.forInputString(Unknown Source)	
at java.lang.Integer.parseInt(Unknown Source)	
at java.lang.Integer.parseInt(Unknown Source)	
at com.teknoscope.java.tutorial.integer.Test.main(Test.java:7)


The above Exception would never be encountered if the base number is in hexadecimal format. However the specified Integer.parseInt method is using the radix 10 thus the exception is thrown. This would be handled properly if we really want to parse the String input as hexadecimal number by invoking the Integer.parseInt(String strVal, int radix). We would be discussing this overloaded method on the succeeding part of this java tutorial.

Java Integer.parseInt maximum and minimum value

There is always a limit on what we could parse as method argument. You might be wondering how to determine the maximum and minimum value. The maximum and minimum value that we could parse can be determine using:

Integer.MAX_VALUE
Integer.MIN_VALUE

Let’s take an example that crosses the minimum and maximum value that we could parse using the Integer.parseInt java method:

System.out.println(Integer.parseInt("-2147483649"));

Running the above code would result to the following:

Exception in thread "main" java.lang.NumberFormatException: For input string: "-2147483649"	
at java.lang.NumberFormatException.forInputString(Unknown Source)	
at java.lang.Integer.parseInt(Unknown Source)	
at java.lang.Integer.parseInt(Unknown Source)	
at com.teknoscope.java.tutorial.integer.Test.main(Test.java:11)

Since we have crossed the minimum value of -2147483648, thus a NumberFormatException is thrown. If you are interested to know the maximum value, it would be 2147483647.We have covered already a lot of things on the first method, let’s consider now the second method of java Integer.parseInt which have the following syntax:

parseInt(String strVal,int radixValue)

The first method only consists of String to parse using the default radix of 10. The method  example Integer.parseInt(“100”) would yield the same result as Integer.parseInt(“100”,10).

Specifying the radix input to Integer.parseInt java method would set the static method to use it as base number in parsing the input string argument. Like for example, if the string value is expected to be in hexadecimal format we would be invoking the method:

Integer.parseInt("A1E",16);

The above example code snippet would treat the string argument in hexadecimal format and eventually convert it to decimal format. The result would be 2590. This is the decimal equivalent of the String input A1E of base 16. One practical use of this method is to convert from one base to another (example is octal to decimal conversion).

The same principle apply as the first method if we are considering the maximum and minimum value. However the method Integer.parseInt(String strVal, int radix), would be a little different since there is radix specified. The maximum and minimum value is still can be determined using the Integer.MAX_VALUE and Integer.MIN_VALUE however the base number should be considered. For example if we are dealing with base 16, the max value would be 7fffffff. I determine the value by this code snippet:

System.out.println(Integer.toHexString((Integer.MAX_VALUE)));

Basically the Integer.toHexString() method is being used only to convert the max value of Integer wrapper class into hexadecimal base number.If the maximum value of Integer object in hexadecimal format, NumberFormatException will be thrown like below.

Exception in thread "main" java.lang.NumberFormatException: For input string: "80000000"	
at java.lang.NumberFormatException.forInputString(Unknown Source)	
at java.lang.Integer.parseInt(Unknown Source)	
at com.teknoscope.java.tutorial.integer.Test.main(Test.java:9)

Integer.parseint() Java Example Code

The following is a source code that shows on the usage of parseInt method of java.lang.Integer class.

package com.javatutorialhq.tutorial;

public class ParseInteger {
    /*
     * This java sample code shows parseInt() 
     * static method in java 
     * Property of javatutorialhq.com 
     * All Rights Reserved 
     * Version 1.0 
     * 04/23/2012
     */
    public static void main(String[] args) {
        int sampleString = Integer.parseInt("1234");
        int sampleRadix = Integer.parseInt("123456", 10);
        int sampleHexRadix = Integer.parseInt("E", 16);
        System.out.println(sampleString);
        System.out.println(sampleRadix);
        System.out.println(sampleHexRadix);
    }
}

Java Example Output:

123412345614


The first output invoke the first method of parseint that takes only one parameter. It simply convert the string numeral into int data type. The second variable sampleRadix, takes a 10 as radix input. It just signifies that the numbering format used is at base 10 which is basically the same as with or without radix output. However the third variable sampleHexRadix takes radix 16. So that means that the input string is at base – 16 or simply in hexadecimal format. Hex value E is equal to 14 in integer.

References: