java.lang.Integer valueOf(String s, int radix)

Description :

This java tutorial shows how to use the valueOf(String s, int radix) method of Integer class under java.lang package. This method return a Integer object which is the equivalent of String input as method argument with respect to the radix specified.

Method Syntax :

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

Parameter Input :

DataType Parameter Description
String s The String object to be converted into Integer Object
int radix the base number to consider in converting the String object

Method Returns :

The valueOf(String s, int radix) method simply returns the Integer equivalent of the String object method object converted from the radix input to base 10.

Compatibility Version :

Requires Java 1.0.2 and up

Exception :

NumberFormatException

– The NumberFormatException will be thrown by the valueOf method if the input String with respect to radix method argument is not a parsable int.

Discussion :

The valueOf(String s, int radix) method is static thus we should invoke it statically for example Integer.valueOf(String s, int radix). This method simply convert the String input of base number radix into Integer object. The radix method argument served as a conversion factor in terms of base number. Make a note that counting numbers are of base 10 which is the default base radix.

If you would have gone through on the rest of integer API you would find the Integer.parseInt(String s, int radix) would be similar in nature. The only difference is that the valueOf(String s, int radix) return an Integer object rather than the primitive type int. So basically if we find the exact equivalent of these two method, you would find the valueOf(String s, int radix) is equivalent to new Integer(Integer.parseInt(String s, int radix).

There is maximum and minimum allowable value to be converted and this is can be determined using the two static method Integer.MAX_VALUE and Integer.MIN_VALUE respectively. If the minimum and maximum value were crossed, a NumberFormatException will be thrown.

Java Code Example :

This java example source code demonstrates the use of valueOf(String s, int radix) method of Integer class. Basically this example asks for a String value and then the radix. After which we used the valueOf method to convert the String into Integer object type.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * valueOf(String s, int radix) method of Integer class
 * convert from any base number to Integer
 */

public class IntegerValueOfStingRadix {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		// Ask user input
		System.out.print("Enter Desired Value:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get the value from user console
		String strValue = scan.nextLine();
		// get the base number from the user input		
		System.out.print("Enter Base Number:");
		scan = new Scanner(System.in);
		int radix = scan.nextInt();
		// close the scanner object
		scan.close();
		// print the value in decimal format
		out.println("Integer Value:" + Integer.valueOf(strValue, 
				radix));

	}

}

Sample Output :

Running the valueOf(String s, int radix) method example source code of Integer class will give you the following output

Enter Desired Value:ABCD
Enter Base Number:16
Integer Value:43981

Exception Scenario :

Enter Desired Value:ABCDE
Enter Base Number:8
Exception in thread "main" java.lang.NumberFormatException: For input string: "ABCDE"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.valueOf(Unknown Source)
	at com.javatutorialhq.java.examples.IntegerValueOfStingRadix.main(IntegerValueOfStingRadix.java:30)

Similar Method :

  • N/A

Suggested Reading List :

References :