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.

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