Java Character getNumericValue Method

JavaJavaBeginner
Practice Now

Introduction

In Java programming language, getNumericValue() is a method that is a part of the Character class. It provides the numerical integer value of a Unicode character. If the specified character does not have any numeric value, the method returns -1. On the other hand, if the character has a numeric value that cannot be represented as a non-negative integer, the method returns -2.

Set up the Java Development Environment

To create and execute Java programs on your computer, you need to set up the Java Development Kit (JDK) on your system. If you do not have a JDK installed on your machine, download and install the latest JDK from the official Oracle download site.

You can check whether the JDK is installed on your system by opening a terminal window (Linux/Mac) or a command prompt (Windows) and running the following command:

java -version

If the JDK is successfully installed, the version of the JDK will be displayed on the console.

Create a Java program file

Create a new file in the ~/project directory with the name CharacterGetNumericValue.java using a text editor or an integrated development environment (IDE) of your choice.

Enter the code shown below in the file:

public class CharacterGetNumericValue {
    public static void main(String[] args) {
        char ch1 = 'A';
        char ch2 = '1';
        char ch3 = '}';
        int numericValue1 = Character.getNumericValue(ch1);
        int numericValue2 = Character.getNumericValue(ch2);
        int numericValue3 = Character.getNumericValue(ch3);

        System.out.println("The integer value of " + ch1 + " : " + numericValue1);
        System.out.println("The integer value of " + ch2 + " : " + numericValue2);
        System.out.println("The integer value of " + ch3 + " : " + numericValue3);
    }
}

This program initializes three characters ch1, ch2, and ch3 with 'A', '1', and '}', respectively. It then gets the integer value of each character using the getNumericValue() method of the Character class and stores them in separate integer variables.

Compile and Run the Program

To compile the Java program, open a terminal window or a command prompt, navigate to the ~/project directory, and execute the following command:

javac CharacterGetNumericValue.java

This will compile the Java file and create a bytecode file named CharacterGetNumericValue.class in the same directory.

Now, run the program by executing the following command:

java CharacterGetNumericValue

This will run the program, and you will see the following output on the console:

The integer value of A : 10
The integer value of 1 : 1
The integer value of } : -1

Here, the first two characters 'A' and '1' have valid integer values, and the method returns the expected results. On the other hand, the third character '}' does not have an integer value, so the method returns -1.

Create a Java program that takes user input

Let's modify the previously created program to take user input and display the integer value of the input character.

Update the code of the CharacterGetNumericValue.java file as shown below:

import java.util.Scanner;

public class CharacterGetNumericValue {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.nextLine().charAt(0);
        int numericValue = Character.getNumericValue(ch);
        System.out.println("The integer value of " + ch + " : " + numericValue);
    }
}

This program first creates a Scanner object to read user input. It then prompts the user to enter a character and reads the input using the nextLine() method of the Scanner class and stores it in the char variable ch.

The program then calls the getNumericValue() method of the Character class to get the integer value of the entered character and stores it in the integer variable numericValue. Finally, it displays the input character and its integer value on the console.

Compile and Run the Program

To compile the modified Java program, open a terminal window or a command prompt, navigate to the ~/project directory, and execute the following command:

javac CharacterGetNumericValue.java

This will compile the Java file and create a bytecode file named CharacterGetNumericValue.class in the same directory.

Now, run the program by executing the following command:

java CharacterGetNumericValue

This will run the program, and you will see the following output on the console:

Enter a character: A
The integer value of A : 10

You can enter any character to get its numeric value.

Summary

In this lab, we have learned how to use the getNumericValue() method of the Character class in Java programming language to get the numerical integer value of a Unicode character. We have also seen how to create Java programs that use this method to get the integer value of user-entered characters.

Other Java Tutorials you may like