Signup/Sign In
LAST UPDATED: AUGUST 27, 2020

Java Integer parseInt (CharSequence s, int beginIndex, int endIndex, int radix) Method

Java parseInt (CharSequence s, int beginText, int endText, int radix) method is a part of the Integer class of the java.lang package. This method returns the signed integer equivalent after the character sequence passed is parsed in accordance with the integer radix value beginning from the passed beginning index and extends to passed (ending index - 1).

Syntax:

public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException

Parameters:

The parameters passed are:

CharSequence s: The character sequence to be parsed.

int beginIndex: The index of the character sequence from where parsing will begin.

int endIndex: The index of the character sequence until where the parsing will extend.

int radix: The integer value according to which the character sequence will be passed.

Returns:

Returns the equivalent integer value obtained after parsing the character sequence in accordance with the integer radix beginning from the begin index and extends till end index-1.

Exception:

NullPointerException: This exception occurs when the input character is null.

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

IndexOutOfBoundsException: This exception takes place when either begin index is negative or begin index value is greater than that of end index value or end index value is greater than s.length().

Example 1:

Here, in this example, the signed integer value of the parsed Character Sequence is returned.

import java.lang.Integer;

public class StudyTonight
{  
        public static void main(String[] args)throws NumberFormatException 
        { 
	        CharSequence s1 = "332";
            CharSequence s2 = "-442";
            int radix = 8;  
            //returns the signed Integer value of the entered string in accordance with the radix and beginning and end index
           
            int res1=Integer.parseInt(s1, 0,1,radix);
            System.out.println(res1); 
            
             int res2=Integer.parseInt(s2,0,2,radix);
            System.out.println(res2);  
           
        }  
          
}  


3
-4

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.lang.Integer;
import java.util.Scanner;

public class StudyTonight
{  
   public static void main(String[] args)
    { 
          try
	        {
            System.out.println("Enter value");
            Scanner sc = new Scanner(System.in);
          
          
	          CharSequence s1 = sc.nextLine();
	          System.out.println("Enter radix");
            int radix = sc.nextInt();
	          System.out.println("Enter start index");
            int si =sc.nextInt();
	          System.out.println("Enter end index");
            int ei = sc.nextInt();
            //returns the signed Integer value of the entered string in accordance with the radix and beginning and end index
           
            int res1=Integer.parseInt(s1,si,ei,radix);
            System.out.println(res1); 
           }  
	        catch(Exception e)
	         {
	          System.out.println("Cannot parse this value");
	         }

           
    }  
          
}  


Enter value
505
Enter radix
8
Enter start index
0
Enter end index
2
40
*********************
Enter value
595
Enter radix
2
Enter start index
0
Enter end index
4
Cannot parse this value



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.