Signup/Sign In
LAST UPDATED: AUGUST 25, 2020

Java Integer decode() Method

Java decode() method belongs to the Integer class. The decode() method is used to decode a string of the form " " into an integer.

The numbers that are acceptable are:

  • decimal

  • hexadecimal

  • octal

Syntax:

public static Integer decode(String s) throws NumberFormatException

Parameter:

The parameter includes the String s which is to be decoded into Integer.

Returns:

the integer value of the Integer object which the String s holds.

The NumberFormatException is thrown in case the string does not contain any parsable integer.

Example 1:

Here, the number starting with '0' will be decoded as octal using the base 8, starting with '0x' will be decoded as hexadecimal using the base 16 and starting with '-0x' will be decoded as signed hexadecimal using the base 16.

import java.lang.Integer;
public class StudyTonight
{  
    public static void main(String[] args)
    {          
         Integer i = 30; 
         
         String s = "300";    
      
        System.out.println(Integer.decode("05234"));   // decoding using base 8             
                        
        System.out.println(Integer.decode("0x980"));   // decoding using base 16               
                        
        System.out.println(Integer.decode("-0x786"));  // decode using base 16 signed 
        
        System.out.println(" Decoded Integer Number for the given string is = " +i.decode(s)); 
       // returns the integer value of the Integer object which the String s holds.

    }  
}  


2716
2432
-1926
Decoded Integer Number for the given string is= 300

Example 2:

Here, we come across NumberFormatException as the string s cannot be decoded into an integer.

import java.lang.Integer;
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        Integer i = 100;          
        String s = "StudyTonight";             
        System.out.println(" Decoded integer number is = " + i.decode(s));   
    }  
}  


Exception in thread "main" java.lang.NumberFormatException: For input string: "StudyTonight"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:740)
at java.lang.Integer.decode(Integer.java:1197)
at StudyTonight.main(StudyTonight.java:7)

Example 3:

Here is a general example where the user can enter the numbers of his choice and get the desired output. The try and catch block is used for handling any exception taking place during the execution of the program.

import java.lang.Integer;
import java.util.Scanner;  
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        try
        {
           System.out.print("Enter the string to be decoded: ");  
           Scanner sc = new Scanner(System.in);  
           String s = sc.nextLine();  
           Integer decoded = Integer.decode(s);  
           System.out.println("Decoded value is:" + decoded);  
        }
        catch(Exception e)
        {
          System.out.println("Number Format Exception,Please Check the Entered String");
        }
  
    }  
}  


Enter the string to be decoded: -0x567
Decoded value is:-1383
************************************************
Enter the string to be decoded: StudyTonight
Number Format Exception,Please Check the Entered String

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.