New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java Integer valueOf(String s, int radix) Method

Java valueOf(String s,int radix) method is a part of the Integer class of the java.lang package. This method is used to return the value of the Integer object of the string value passed as an argument when it is parsed using the integer radix value passed as an argument.

It must be noted that the value returned by this method can be interpreted as new Integer(Integer.parseInt(s,radix)).

Syntax:

public static Integer valueOf(String s, int radix) throws NumberFormatException  

Parameters:

The parameter passed is the string whose equivalent value of the 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 of the Integer object of the String value passed as a parameter in accordance with the radix value.

Example 1:

Here, the Integer object 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 s = "603";  
            int radix = 16;  
            System.out.print("\nEntered value and Base value is: " + s + " and " + radix);  
           //returns the Integer object of the entered string in accordance with the radix            
            System.out.println("\nEquivalent Integer object is " + Integer.valueOf(s, radix)); 
        }  
}  


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();
          System.out.println("Equivalent Integer object : " +Integer.valueOf(s,radix)); //returns the Integer object value of the string with respect to radix 
        }
        catch(NumberFormatException e)
        {
          System.out.println("Invalid Input!!");
        }         
    }  
}  


Enter the value: 631
Enter the radix: 8
Equivalent Integer object : 409
********************************
Enter the value: 0x678
Enter the radix: 16
Invalid Input!!

Live Code:

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.