Signup/Sign In
LAST UPDATED: OCTOBER 5, 2020

Java Long parseLong (CharSequence s, int begin, int end, int radix) Method

Java parseLong(CharSequence s, int beginText, int endText, int radix) method is a part of the Long class of the java.lang package. This method returns the signed long 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 long parseLong(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 long 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 the begin index value is greater than that of the end index value or end index value is greater than s.length().

Example 1:

Here, in this example, the signed long value of the parsed character sequence is returned.

import java.lang.Long;

public class StudyTonight
{  
    public static void main(String[] args)throws NumberFormatException 
    { 
	    CharSequence s1 = "332";
        CharSequence s2 = "-442";
        int radix = 8;  
        //returns the signed long value of the entered string in accordance with the radix and beginning and end index
           
        long res1 = Long.parseLong(s1, 0,1,radix);
        System.out.println(res1); 
            
        long res2 = Long.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.Long;
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 Long value of the entered string in accordance with the radix and beginning and end index
           
            long res1 = Long.parseLong(s1,si,ei,radix);
            System.out.println(res1); 
        }  
	    catch(Exception e)
	    {
	        System.out.println("Cannot parse this value");
	    }   
    }         
}  


Enter value
543
Enter radix
8
Enter start index
0
Enter end index
2
44
**********************
Enter value
6767
Enter radix
16
Enter start index
0
Enter end index
3
1654
**********************
Enter value
6787
Enter radix
3
Enter start index
5
Enter end index
6
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.