Home » Java Tutorial » ParseInt Java

ParseInt Java

In java language, parseint Java is a method for converting the string value to integer value.

In java language, there is a method for converting the string value to integer value. But what is the point in doing this task when you can take input directly in integer form?

As we are familiar with many different datatypes in programming languages, if we keep all the big database data in all traditional datatypes format, then it would be difficult for a new person to predict the datatype of particular data and this will be very frustrating. So, may database engineers keep the data in the form of strings, all the different types of data is converted so that it would be easy to retrieve data.

So now, let us think of a database with all values as strings. Now, we need to retrieve a value whose data type is an integer, for doing so we have this method parseInt(). It will convert the given data to integer datatype.This method belongs to Integer class in java which comes under java.lang package. So, with the help of parseInt method of integer class, we can have type conversion of data. There are similar classes and methods for every other datatype as well, but we are going to mainly focus on parseInt java.

How to use Integer.parseInt java method?

Java parseInt method is a method of class integer, so to use it, we use the dot operator. Here, Integer class is a wrapper class, which means that we use an object of the datatype class to store the value. As java is an Object Oriented Programming language, we use objects in every place. For datatype, we use wrapper class which is the class of datatype and we can store value in the object of that datatype class.

So for example, if we have a string “10” for the integer value and we want to convert it to an integer, then we can use parseInt method as:

Int x = Integer.parseInt(“10”);

// x will store the integer value 10

As we can see that using dot operator we used the parseInt method of Integer class for converting the string value.

Let us use it in a code to understand it better


import java.util.Scanner;
public class Example1
  		public static void main(String[] args)
// We create a scanner object to take input from the user
  		      	 Scanner myObj = new Scanner(System.in);
  	          	      	// we take input of a numeric value as a string
  	          	      	String s1 = myObj.nextLine();
  		      	// print the input numeric value plus 10
  	          	      	System.out.println("Before using java parseInt: " + (s1+10));
  		      	 // Now we convert the numeric input value to integer and store in variable a
      	      	int a = Integer.parseInt(s1);
  		      	// The integer value is displayed after type conversion plus 10
  		      	System.out.println("After using java parseInt: " + (a+10));


Before using java parseInt: 10010
After using java parseInt: 110

This is a basic program to understand the use of parseInt method. First, we took the input of the numeric value as a string and displayed it by adding numeric value 10 to it. But as it was in a string format, it took “+” sign as concatenation operator and joined the number 10 with the input 100 giving output 10010. This type of errors can occur in real-time, so to avoid such mistakes by using the parseInt method and converting the string to integer, we can add any number to get the desired output as we got here as 110 after the use of java parseInt method.

What errors can occur while using java parseInt method?

The java parseInt method only works when we pass the string of the numeric value, but if we pass a character value in the parseInt method then it will raise a NumberFormatException. So, we need to make sure about the value we pass to the parseInt function. It is not always the case as we can have character value but for that, we need to learn about the other parameter that we can pass in the parseInt method.

Parameters of java parseInt method

As we already know about the first parameter of java integer parseInt method is the string value, the second parameter is the radix value. We can specify the number system we want to convert a string to. So using the second parameter which is radix we can use a character value and can convert it to some valid number system. For a character or string value, we can use radix value 29 and convert it to a numeric value.

So, in this way, we can choose the radix in which we want to convert a string value in.


public class Example2
	public static void main(String[] args)
    	   // Using parseInt to convert value with radix 8
     	  int a = Integer.parseInt("123", 8);
     	  System.out.println("Value of 123 with radix 8: "+a);
    	   // Using parseInt to convert value with radix 16
     	  int b = Integer.parseInt("123", 16);
     	  System.out.println("Value of 123 with radix 16: "+b);
    	   // Using parseInt to convert value with radix 12
     	  int c = Integer.parseInt("123", 12);   	
     	  System.out.println("Value of 123 with radix 12: "+c);


Value of 123 with radix 8: 83
Value of 123 with radix 16: 291
Value of 123 with radix 12: 171

In this example, with the string value, we also specified the radix value in which we want our output. So for the string value “123” we got output 83 in radix 8, 291 in radix 16, 171 in radix 12.


Radix 8: 1x8^2 + 2x8^1 + 3x8^0 = 83
Radix 16: 1x16^2 + 2x16^1 + 3x16^0 = 291
Radix 12: 1x12^2 + 2x12^1 + 3x12^0 = 171


//This example is for using radix value 29
public class Example3
    public static void main(String[] args)
    	   // Using parseInt to convert value with radix 29
     	  int a = Integer.parseInt("a", 29);
     	  System.out.println("Value of a with radix 29: "+a);


Value of a with radix 29: 10

As the number system with radix 29 has alphabetical values after 0 to 9 which start from “a” and goes on till “s”. So, we can only use character value or string value if it is in the range of radix value 29.

Do you know?
1. Java Scanner with Examples
2. Java Stack with Examples
3. ParseInt Java
4. Try Catch JAVA
5. For loop Java with Examples
6. Java vs Javascript
7. Java Iterator with Examples
8. Substring Java
9. Java Switch Statement with Examples
10. Java Queue

Pin It on Pinterest