Signup/Sign In

Java Integer parseInt(String s, int radix) Method

Java parseInt(String s, int radix) method is the part of the Integer class of the java.lang package. This method is used to parse the string value as a signed decimal Integer object in the specified integer radix value passed as an argument.

It must be noted that the characters passed in the string must be all decimal except for the first character which is used to define the sign of the integer. It may be an ASCII minus '-' sign to depict a negative value or an ASCII plus '+' sign to depict a positive value.

Syntax:

public static int parseInt (String s, int radix)  

Parameters:

The parameter passed is the string value whose signed decimal Integer object is to be returned and the integer radix value according to which the input string will be parsed.

Exception:

NumberFormatException: This exception occurs when the input string is not parsable.

Returns:

Returns the value as the signed decimal Integer object of the String value passed as a parameter in accordance with the radix value.

Example 1:

Here, the signed decimal integer representation is returned of the String value in accordance with the integer radix passed.

import java.lang.Integer;

public class StudyTonight {
  public static void main(String[] args) throws NumberFormatException {
    String s1 = "603";
    String s2 = "-603";
    int radix = 16;
    System.out.print("\nEntered value and Base value is: " + s1 + " and " + radix);
    //returns the signed Integer object of the entered string in accordance with the radix            
    System.out.println("\nEquivalent Integer object is " + Integer.parseInt(s1, radix));

    System.out.print("\nEntered value and Base value is: " + s2 + " and " + radix);
    //returns the signed Integer object of the entered string in accordance with the radix            
    System.out.println("\nEquivalent Integer object is " + Integer.parseInt(s2, radix));
  }
}


Entered value and Base value is: 603 and 16
Equivalent Integer object is 1539

Entered value and Base value is: -603 and 16
Equivalent Integer object is -1539

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;

public class StudyTonight 
{
  public static void main(String[] args) 
  {
    try 
    {
      System.out.print("Enter the value: ");
      Scanner sc = new Scanner(System. in );
      String s = sc.nextLine();
      System.out.print("Enter the radix: ");
      int radix = sc.nextInt();
      //parse the string value into signed value with respect to radix 
      System.out.println("Equivalent Integer object : " + Integer.parseInt(s, radix)); 
    }
    catch(NumberFormatException e) {
      System.out.println("Invalid Input!!");
    }
  }
}


Enter the value: 57
Enter the radix: 16
Equivalent Integer object : 87
*********************************
Enter the value: -37
Enter the radix: 16
Equivalent Integer object : -55
*********************************
Enter the value: 0x679
Enter the radix: 2
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.