Introduction
Java toString()
method is used to convert Character Object into String. In this lab, you will learn how to use the Character toString()
method in Java programming.
Java toString()
method is used to convert Character Object into String. In this lab, you will learn how to use the Character toString()
method in Java programming.
Create a new Java file named CharToString.java
in the ~/project
directory using the command:
cd ~/project
touch CharToString.java
Open the file CharToString.java
in a text editor of your choice.
In this step, you will create a Character object and initialize it with a value.
// CharToString.java
public class CharToString {
public static void main(String[] args) {
// create Character object
Character ch = 'a';
}
}
In this example, we created a Character object named ch
and assigned the value 'a' to it.
In this step, you will use the toString()
method to convert the Character object into a String.
// CharToString.java
public class CharToString {
public static void main(String[] args) {
// create Character object
Character ch = 'a';
// convert Character object to String
String str = ch.toString();
}
}
In this example, we called the toString()
method on the Character object ch
and assigned the returned String value to a new variable named str
.
In this step, you will output the result of the toString()
method.
// CharToString.java
public class CharToString {
public static void main(String[] args) {
// create Character object
Character ch = 'a';
// convert Character object to String
String str = ch.toString();
// output the result
System.out.println("The String representation of the Character object is: " + str);
}
}
In this example, we used the System.out.println()
method to output the result of the toString()
method.
In this step, you will create an example that allows the user to input a Character value and then converts it to a String.
// CharToString.java
import java.util.Scanner;
public class CharToString {
public static void main(String[] args) {
try {
System.out.print("Enter a Character value: ");
Scanner sc = new Scanner(System.in);
Character ch = sc.next().charAt(0);
String str = ch.toString();
System.out.println("The String representation of the Character object is: " + str);
}
catch(Exception e) {
System.out.println("Invalid input!!");
}
}
}
In this example, we used Scanner
to get input from the user and then converted the input Character value to a String using the toString()
method.
After writing the above code, save and close the file.
To compile the code, run the following command from the ~/project
directory:
javac CharToString.java
To run the code, run the following command:
java CharToString
Congratulations! You have successfully learned how to use the Java toString()
method to convert a Character object into a String. Now you know how to create a Character object, call the toString()
method, and output the result. You have also learned how to create an example that allows users to input a Character value and then convert it to a String.