ASCII value of character

The term ASCII (American Standard Code for Information Interchange) refers to a defined character encoding. It assigns certain codes to characters such as letters, digits and punctuation marks. The ASCII code is used to specify how the characters are displayed by electronic devices – such as PCs or smartphones. This is an important part of many programming operations.

Code Explanation

LineDescription
5Initializes the variable reader of type Scanner with a new Scanner object with the input Parameter System.in (to read from the standard input)
6Declares the variables character of type char
7Declares the variables asciiValue of type int
9 – 10Prompts the user to enter a character of type char. The value is stored in the variable named character
13In this line of code the integer value of the character of type char is stored in the variable asciiValue
14 – 15Outputs the ASCII value of the character by outputting the Variable asciiValue
import java.util.Scanner;

class AsciiValue {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        char character;
        int asciiValue;

        System.out.print("Please enter character: ");
        character = reader.next().charAt(0);
        reader.close();

        asciiValue = (int) character;
        System.out.println("ASCII value of the character "
                + character + " is: " + asciiValue);
    }
}
Output
Please enter character: F
ASCII value of the character F is: 70