Shahiduzzaman Shahid Shahiduzzaman Shahid - 1 month ago 7
Java Question

How to take character input in java

In C, we are able to take input as character with the keyword

char
from keyboard as

scanf("%c", &ch);


But In Java how to do this?

I have tried this:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char c = scanner.next().charAt(0);
System.out.println("You have entered: "+c);
}
}

Answer

use the System class

char yourChar = System.in.read()
Comments