Java Character getName Method

JavaJavaBeginner
Practice Now

Introduction

The getName() method in Java is a part of the Character class which returns the Unicode name (if available) assigned to the character represented by a given Unicode code point. This method is very useful while representing the Unicode characters in your Java program.

Set Up

Create a file named CharGetName.java in the ~/project directory using the command:

touch ~/project/CharGetName.java

Open the CharGetName.java file in your preferred text editor.

Importing the Required Class

In the first line of the code, we need to import the required class that is Character class. We will import it as below-

import java.lang.Character;

Define a Class

Define a class CharGetName and implement the main() method. In the main() method, we will perform the operation of getting the Unicode name of different characters like "H", "f", etc in Java.

Full code looks like below-

import java.lang.Character;

public class CharGetName {
    public static void main(String[] args) {
        int codepoint1 = 72; // H
        int codepoint2 = 102; // f
        int codepoint3 = 0;

        String name1 = Character.getName(codepoint1);
        String name2 = Character.getName(codepoint2);
        String name3 = Character.getName(codepoint3);

        System.out.println("The name of character " + Character.toChars(codepoint1)[0] + " is "+name1);
        System.out.println("The name of character " + Character.toChars(codepoint2)[0] + " is "+name2);
        System.out.println("The name of character with codepoint 0 is "+ name3);
    }
}

In this step, we have defined a class named CharGetName with the main method inside it. In the main method, we have created an integer variable with the Unicode code point of characters as its values: codepoint1 having a value of 72(H) and codepoint2 having a value of 102 (f). We have used the Character.getName() method to get the names of the characters and then we have printed the name of the characters using System.out.println() method.

Compile and Run the Java Program

Compile the CharGetName Java file using the following command:

javac CharGetName.java

Once the compilation process completes successfully, run the java code by using the following command:

java CharGetName

Editing the Input Value

Now, let's create a program that will take the user input and return the name of the character on a console.

import java.util.Scanner;
import java.lang.Character;

public class CharGetName {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        int codepoint = ch;
        String name = Character.getName(codepoint);

        System.out.println("The name of character "+ch+" is "+name);
    }
}

Here, a Scanner object is created to get input from the user. We are using the Scanner.next() method to get the input value, and then, we are converting the entered character to an integer codepoint by using the Character() method in order to find its name. After that, we have printed the name of the character by making use of System.out.println() method.

Compile and Run the Java Program

Compile the CharGetName Java file using the following command:

javac CharGetName.java

Once the compilation process completes successfully, run the java code by using the following command:

java CharGetName

Now, enter the character whose name you want to find. For example, enter "A". You will get the name of that character on the console.

Using Invalid Character as Input

Now, let's see how the program will respond if we enter an invalid input.

import java.util.Scanner;
import java.lang.Character;

public class CharGetName {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);

        if(!Character.isDefined(ch)) {
            System.out.println("\nINVALID INPUT: Please enter a valid character.");
            return;
        }

        int codepoint = ch;
        String name = Character.getName(codepoint);

        System.out.println("\nThe name of character "+ch+" is "+name);
    }
}

In this program, we are checking whether the entered character is defined or not. If the entered character is not defined, then we will print a message to the console saying "Invalid input: Please enter a valid character." and if the character is defined then we will get its name like before and print it out with a proper message.

Compile and Run the Java Program

Compile the CharGetName Java file using the following command:

javac CharGetName.java

Once the compilation process completes successfully, run the java code by using the following command:

java CharGetName

Now, enter an invalid input like "8" or "$". You will get an error message saying "INVALID INPUT: Please enter a valid character.".

Using an Unassigned Character as Input

Now, let's see how the program will respond if we use an unassigned character as input.

import java.util.Scanner;
import java.lang.Character;

public class CharGetName {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);

        if(!Character.isDefined(ch)) {
            System.out.println("\nINVALID INPUT: Please enter a valid character.");
            return;
        }

        String name = Character.getName(ch);

        if(name == null) {
            System.out.println("\nNO NAME FOUND: There is no assigned name for this character.");
            return;
        }

        System.out.println("\nThe name of character "+ch+" is "+name);
    }
}

Here, we are making use of the Character.getName(ch) method instead of using Character.getName(codepoint). If the entered character does not have a name assigned then it will return null. So, we are checking if the returned name is null or not. If it is null, then we will print a message saying "NO NAME FOUND: There is no assigned name for this character."

Compile and Run the Java Program

Compile the CharGetName Java file using the following command:

javac CharGetName.java

Once the compilation process completes successfully, run the java code by using the following command:

java CharGetName

Now, enter an unassigned character like null ("\u0000") and you will get a message saying "NO NAME FOUND: There is no assigned name for this character."

Summary

In this lab, we learned about the getName() method in Java and how to use it to get the Unicode name of characters by implementing Java code examples. We also learned how to read user input for it and how to handle errors by creating and running a Java program in the terminal of Ubuntu system.

Other Java Tutorials you may like