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.


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/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") 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/comments("`Comments`") 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/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117586{{"`Convert Character to String`"}} java/classes_objects -.-> lab-117586{{"`Convert Character to String`"}} java/class_methods -.-> lab-117586{{"`Convert Character to String`"}} java/modifiers -.-> lab-117586{{"`Convert Character to String`"}} java/oop -.-> lab-117586{{"`Convert Character to String`"}} java/user_input -.-> lab-117586{{"`Convert Character to String`"}} java/wrapper_classes -.-> lab-117586{{"`Convert Character to String`"}} java/identifier -.-> lab-117586{{"`Convert Character to String`"}} java/arrays -.-> lab-117586{{"`Convert Character to String`"}} java/comments -.-> lab-117586{{"`Convert Character to String`"}} java/data_types -.-> lab-117586{{"`Convert Character to String`"}} java/operators -.-> lab-117586{{"`Convert Character to String`"}} java/output -.-> lab-117586{{"`Convert Character to String`"}} java/strings -.-> lab-117586{{"`Convert Character to String`"}} java/variables -.-> lab-117586{{"`Convert Character to String`"}} java/object_methods -.-> lab-117586{{"`Convert Character to String`"}} java/string_methods -.-> lab-117586{{"`Convert Character to String`"}} java/system_methods -.-> lab-117586{{"`Convert Character to String`"}} end

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