Java Long toString Exploration

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the toString() method of the Long class in Java. We will look into the syntax of this method and understand the parameters it takes and the values it returns. Finally, we will write and execute some example programs to understand the working of the toString() method.

Import required package and class

In order to use the Long class and its method, we need to import the package java.lang. The Long.toString() method returns the String representation of a long value with respect to the radix provided. In this step, we will import the package java.lang and create a class LongToString of public type.

import java.lang.*;

public class LongToString {
    // class code
}

Create a method to demonstrate converting long to string

In this step, we will create a public static method convertLongToString() inside the LongToString class. This method will take two parameters - a long value l and a radix value r which define the base for the String conversion. Finally, the method will return a String value which contains the string representation of the long value.

public static String convertLongToString(long l, int r) {
    String s = Long.toString(l, r);
    return s;
}

Call convertLongToString() method with different values

In this step, we will call the convertLongToString() method multiple times with different long and radix values. We will print the returned string to the console.

public static void main(String[] args) {
    // using decimal radix (10)
    long l1 = 2584L;
    int r1 = 10;
    System.out.println("Decimal Equivalent of " + l1 + ": " + convertLongToString(l1, r1)); //  output: "Decimal Equivalent of 2584: 2584"

    // using hexadecimal radix (16)
    long l2 = 255L;
    int r2 = 16;
    System.out.println("Hexadecimal Equivalent of " + l2 + ": " + convertLongToString(l2, r2)); // output: "Hexadecimal Equivalent of 255: ff"

    // using octal radix (8)
    long l3 = 133;
    int r3 = 8;
    System.out.println("Octal Equivalent of " + l3 + ": " + convertLongToString(l3, r3)); //output: "Octal Equivalent of 133: 205"
}

Code to handle Exceptions

In the preceding steps, we did not handle the exception when an invalid input is provided from the user. In this step, we will handle any exception by using the try-catch block.

import java.util.Scanner;
import java.lang.*;

public class LongToString {
    public static String convertLongToString(long l, int r) {
        String s = Long.toString(l, r);
        return s;
    }

    public static void main(String[] args) {
        try {
            System.out.println("Enter a long value and a radix: ");
            Scanner sc = new Scanner(System.in);
            long num = sc.nextLong();
            int radix = sc.nextInt();
            System.out.println(convertLongToString(num, radix));
        } catch (Exception e) {
            System.out.println("Invalid input. " + e.getMessage());
        }
    }
}

Compile and execute the code

When we run the code, we can call the main() method of the LongToString class. It will call the convertLongToString() method to perform string conversion. We can compile the Java code and execute it in the terminal. To do this, follow the below steps:

  1. Open terminal
  2. Navigate to the directory where the LongToString.java file is stored using the cd command.
  3. Compile the code using the javac LongToString.java command and hit Enter.
  4. Execute the code using the java LongToString command and hit Enter.
  5. When the code runs, it will prompt the user to enter the long value and the radix value in separate input lines.
  6. Once the user enters the values, the program will output the String equivalent of the entered long value with respect to the entered radix.
$ cd ~/project
$ javac LongToString.java
$ java LongToString
Enter a long value and a radix:
377
16
17f

Summary

In this lab, we learned about the Long.toString() method of the Long class in Java. We learned about the syntax of the toString() method and the parameters it takes. We also wrote and executed some example programs where the toString() method was used to convert long values to their equivalent String representations with respect to a given radix value. We also learned how to handle exceptions while getting input from the user.

Other Java Tutorials you may like