Convert String To Long in Java

In Java, converting a String to a Long is a common operation when dealing with numeric values provided as strings. In this blog post, we will explore different methods to convert a String to a Long and provide practical examples with output to illustrate each approach. 

Method 1: Using Long.parseLong() Method 

The Long.parseLong() method is a straightforward way to convert a String to a Long. It parses the string argument as a signed long and returns the resulting numeric value. 

Example:

public class StringToLongExample {
    public static void main(String[] args) {
        String strNumber = "1234567890";

        try {
            // Convert String to Long using parseLong()
            long longValue = Long.parseLong(strNumber);
            System.out.println("Input String: " + strNumber);
            System.out.println("Long Value: " + longValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input for long conversion");
        }
    }
}

Output:

Input String: 1234567890
Long Value: 1234567890

Method 2: Using Long.valueOf() Method 

The Long.valueOf() method converts a String to a Long object. It returns a Long instance representing the specified long value if the String is a valid representation of a long. 

Example:

public class StringToLongExample {
    public static void main(String[] args) {
        String strNumber = "9876543210";

        try {
            // Convert String to Long using valueOf()
            Long longValue = Long.valueOf(strNumber);
            System.out.println("Input String: " + strNumber);
            System.out.println("Long Value: " + longValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input for long conversion");
        }
    }
}

Output:

Input String: 9876543210
Long Value: 9876543210

Method 3: Using NumberFormat.parse() Method 

You can also use NumberFormat to parse the String and convert it to a Long. 

Example:

import java.text.NumberFormat;
import java.text.ParseException;

public class StringToLongExample {
    public static void main(String[] args) {
        String strNumber = "2147483647";

        // Convert String to Long using NumberFormat
        try {
            NumberFormat numberFormat = NumberFormat.getInstance();
            long longValue = numberFormat.parse(strNumber).longValue();
            System.out.println("Input String: " + strNumber);
            System.out.println("Long Value: " + longValue);
        } catch (ParseException e) {
            System.out.println("Error: Invalid input for long conversion");
        }
    }
}

Output:

Input String: 2147483647
Long Value: 2147483647

Method 4: Using Scanner class 

The Scanner class can also be used to convert a String to a Long. 

Example:

import java.util.Scanner;

public class StringToLongExample {
    public static void main(String[] args) {
        String strNumber = "9223372036854775807";

        // Convert String to Long using Scanner
        try {
            Scanner scanner = new Scanner(strNumber);
            long longValue = scanner.nextLong();
            System.out.println("Input String: " + strNumber);
            System.out.println("Long Value: " + longValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input for long conversion");
        }
    }
}

Output:

Input String: 9223372036854775807
Long Value: 9223372036854775807

Conclusion

Converting a String to a Long is a frequent requirement in Java programming. You can achieve this using methods like Long.parseLong(), Long.valueOf(), NumberFormat, or Scanner. In this blog post, we provided practical examples with outputs to demonstrate each method's effectiveness. Always ensure proper error handling to handle invalid input gracefully and avoid potential exceptions.

Related String Conversion Examples

Comments