Convert Character to String

JavaJavaBeginner
Practice Now

Introduction

The toString(char c) method is part of the Character class in Java, which returns the equivalent String object of the specified character value. This method is used to convert a char value into a String representation. This lab will help you understand how to use this method in Java.

Setting up the code file

Open a terminal window and create a new file named CharToString.java using the following command:

touch CharToString.java

Open the file CharToString.java in a text editor or an Integrated Development Environment (IDE) of your choice.

Writing the java code to convert a char to a String

In this step, you will write the Java code to convert a char to a String representation using the toString(char c) method.

public class CharToString {

    public static void main(String[] args) {

        // Step 1: Declare a character variable
        char ch = 'A';

        // Step 2: Convert char to String
        String str = Character.toString(ch);

        // Step 3: Output the results
        System.out.println("The character is: " + ch);
        System.out.println("The string is: " + str);

    }
}

The above code first declares a character variable named ch with a value of A. It then converts the character ch to a String representation using the toString method and saves it to a variable named str. Finally, it outputs both the character and the string representation of the character.

Compiling and Running the code

To compile the code, go to the terminal and navigate to the ~/project directory. Then, run the following command:

javac CharToString.java

This will compile the Java code and generate a class file named CharToString.class. To run the program, type the following command.

java CharToString

This will execute the Java program and will output the character and its string representation.

Testing with user input

The toString(char c) method can be used to convert any char variable to its string representation. Let's test this by allowing the user to input a character and converting it to a string.

public class CharToString {

    public static void main(String[] args) {

        // Step 1: Declare a Scanner object for user input
        Scanner input = new Scanner(System.in);

        // Step 2: Prompt the user for input
        System.out.println("Enter a character:");

        // Step 3: Read the input character
        char ch = input.next().charAt(0);

        // Step 4: Convert char to String
        String str = Character.toString(ch);

        // Step 5: Output the results
        System.out.println("The character is: " + ch);
        System.out.println("The string is: " + str);

    }
}

In this code, an instance of the Scanner class is used to read user input and the charAt(0) method is used to get the first character of the input string. Now let's compile and run the code. Use the following command:

javac CharToString.java && java CharToString

This will compile and execute the program. You should see the program prompting the user for input and then converting the entered character to its string representation.

Summary

In this lab, you learned how to use the toString(char c) method of the Character class in Java to convert a char value to its String representation. You also learned how to write Java code to get user input and convert the input character to its String representation.

Other Java Tutorials you may like