Java Long parseLong Method

JavaJavaBeginner
Practice Now

Introduction

The parseLong(String s) method is a static method of the Long class in Java. It is used to convert a string value to a signed decimal long value. In this lab, you will learn how to use this method to convert string values to long values.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117898{{"`Java Long parseLong Method`"}} java/classes_objects -.-> lab-117898{{"`Java Long parseLong Method`"}} java/class_methods -.-> lab-117898{{"`Java Long parseLong Method`"}} java/exceptions -.-> lab-117898{{"`Java Long parseLong Method`"}} java/modifiers -.-> lab-117898{{"`Java Long parseLong Method`"}} java/oop -.-> lab-117898{{"`Java Long parseLong Method`"}} java/packages_api -.-> lab-117898{{"`Java Long parseLong Method`"}} java/user_input -.-> lab-117898{{"`Java Long parseLong Method`"}} java/wrapper_classes -.-> lab-117898{{"`Java Long parseLong Method`"}} java/identifier -.-> lab-117898{{"`Java Long parseLong Method`"}} java/arrays -.-> lab-117898{{"`Java Long parseLong Method`"}} java/data_types -.-> lab-117898{{"`Java Long parseLong Method`"}} java/operators -.-> lab-117898{{"`Java Long parseLong Method`"}} java/output -.-> lab-117898{{"`Java Long parseLong Method`"}} java/strings -.-> lab-117898{{"`Java Long parseLong Method`"}} java/variables -.-> lab-117898{{"`Java Long parseLong Method`"}} java/string_methods -.-> lab-117898{{"`Java Long parseLong Method`"}} java/system_methods -.-> lab-117898{{"`Java Long parseLong Method`"}} end

Create a LongParseLong class

Create a class named LongParseLong.

public class LongParseLong {

}

Declare the main method

Declare the main method inside the LongParseLong class.

public class LongParseLong {
    public static void main(String[] args) {

    }
}

Convert a string to a long

In the main method, convert a string value to a long value using the parseLong method.

public class LongParseLong {
    public static void main(String[] args) {
        String str = "123";
        long num = Long.parseLong(str);
        System.out.println("The string value " + str + " is converted to the long value " + num);
    }
}

In the above code, the parseLong method is used to convert the string "123" to the long value 123. The variable num stores the converted long value, which is then printed to the console.

Handle invalid input

Handle invalid input using a try-catch block.

public class LongParseLong {
    public static void main(String[] args) {
        String str = "abc";
        try {
            long num = Long.parseLong(str);
            System.out.println("The string value " + str + " is converted to the long value " + num);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input: " + str + " cannot be converted to a long");
        }
    }
}

In the above code, the string "abc" is not a valid input for the parseLong method, as it cannot be converted to a long value. Therefore, a NumberFormatException is thrown. This exception is caught by the catch block, which prints an error message to the console.

Convert a negative string to a long

Convert a negative string value to a long value using the parseLong method.

public class LongParseLong {
    public static void main(String[] args) {
        String str = "-123";
        try {
            long num = Long.parseLong(str);
            System.out.println("The string value " + str + " is converted to the long value " + num);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input: " + str + " cannot be converted to a long");
        }
    }
}

In the above code, the string "-123" is a negative value and can be converted to a long value using the parseLong method. The resulting long value -123 is printed to the console.

Use a scanner to get user input

Use a Scanner object to get user input and convert it to a long value.

import java.util.Scanner;

public class LongParseLong {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string to convert to a long: ");
        String str = scanner.nextLine();
        try {
            long num = Long.parseLong(str);
            System.out.println("The string value " + str + " is converted to the long value " + num);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input: " + str + " cannot be converted to a long");
        }
    }
}

In the above code, a Scanner object is created to get user input from the console using the nextLine method. The input string is then passed to the parseLong method to convert it to a long value, which is then printed to the console.

Convert a hexadecimal string to a long

Convert a hexadecimal string value to a long value using the parseLong method.

public class LongParseLong {
    public static void main(String[] args) {
        String str = "0x7fffffff";
        try {
            long num = Long.parseLong(str, 16);
            System.out.println("The hexadecimal string value " + str + " is converted to the long value " + num);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input: " + str + " cannot be converted to a long");
        }
    }
}

In the above code, the parseLong method is used to convert the hexadecimal string "0x7fffffff" to a long value. The second parameter of the parseLong method specifies the radix, which is 16 for hexadecimal values. The resulting long value 2147483647 is printed to the console.

Convert a binary string to a long

Convert a binary string value to a long value using the parseLong method.

public class LongParseLong {
    public static void main(String[] args) {
        String str = "1111111111111111111111111111111";
        try {
            long num = Long.parseLong(str, 2);
            System.out.println("The binary string value " + str + " is converted to the long value " + num);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input: " + str + " cannot be converted to a long");
        }
    }
}

In the above code, the parseLong method is used to convert the binary string "1111111111111111111111111111111" to a long value. The second parameter of the parseLong method specifies the radix, which is 2 for binary values. The resulting long value 4294967295 is printed to the console.

Convert a string with a type indicator to a long

Convert a string with a type indicator to a long value using the parseLong method.

public class LongParseLong {
    public static void main(String[] args) {
        String str = "123L";
        try {
            long num = Long.parseLong(str);
            System.out.println("The string value " + str + " is converted to the long value " + num);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input: " + str + " cannot be converted to a long");
        }
    }
}

In the above code, the string "123L" contains a type indicator for a long value. However, the parseLong method only accepts signed decimal values and throws a NumberFormatException when it encounters the type indicator. Therefore, the catch block is executed and an error message is printed to the console.

Summary

Congratulations! You have completed the Java Long parseLong (String S) Method lab. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like