Java Program to character input using the Scanner class


In this program, You will learn how to character input using the Scanner class in java.


char ch = sc.next().charAt(0);

Example: How to character input using the Scanner class in java.

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        char ch;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character value:");
        ch = sc.next().charAt(0);

        System.out.println("Input character is:" + ch);

    }
}

Output:

Enter a character value:H
Input character is:H