How can a char be entered in Java from keyboard?
-
Duplicate? - http://stackoverflow.com/questions/811851/how-do-i-read-input-character-by-character-in-java – Petar Minchev Jun 15 '10 at 07:37
-
why do you start every question name with "question about"? This is redundant. – Pavel Radzivilovsky Jun 15 '10 at 07:46
-
read a char without waiting a carriage return: https://stackoverflow.com/questions/62122059/in-java-is-it-possible-to-system-in-read-reads-a-key-without-waiting-a-carriage – danilo May 31 '20 at 23:46
8 Answers
You can either scan an entire line:
Scanner s = new Scanner(System.in);
String str = s.nextLine();
Or you can read a single char
, given you know what encoding you're dealing with:
char c = (char) System.in.read();

- 161,610
- 92
- 305
- 395
-
4`char c = (char) System.in.read();` (casting a byte from system encoding to UTF-16) will only work if the characters have identical values in both encodings; this will usually only work for a very small range of characters. Using `Scanner` is more reliable. – McDowell Jun 15 '10 at 08:14
-
18`System.in.read()` may block until the user types a carriage return (at least on Windows) – user85421 Apr 06 '16 at 07:50
-
2
You can use Scanner like so:
Scanner s= new Scanner(System.in);
char x = s.next().charAt(0);
By using the charAt function you are able to get the value of the first char without using external casting.

- 181
- 1
- 2
-
1This has the same problem as the accepted answer: it will block until the user types the carriage return (at least on Windows) – yannick1976 Apr 21 '20 at 16:28
-
Using nextline and System.in.read as often proposed requires the user to hit enter after typing a character. However, people searching for an answer to this question, may also be interested in directly respond to a key press in a console!
I found a solution to do so using jline3, wherein we first change the terminal into rawmode to directly respond to keys, and then wait for the next entered character:
var terminal = TerminalBuilder.terminal()
terminal.enterRawMode()
var reader = terminal.reader()
var c = reader.read()
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.12.3</version>
</dependency>

- 151
- 2
- 6
-
-
-
I used this to respond to the keys 'i' 'j' 'k' 'l' to navigate left right up down. No need to press enter. – drcicero Nov 12 '20 at 15:48
-
Did you by any chance write out something in between and flush the buffer? – Pica Nov 12 '20 at 19:04
-
Does that make a difference? Interesting! Yes, I basically reprinted the whole screen (width x height) :) – drcicero Nov 13 '20 at 20:50
-
I tried version 3.23.0 and it works great with Linux (didn't try Windows or MacOS); grabs single character, no Enter/Return key needed. – Ron McLeod Aug 27 '23 at 06:01
You can use a Scanner
for this. It's not clear what your exact requirements are, but here's an example that should be illustrative:
Scanner sc = new Scanner(System.in).useDelimiter("\\s*");
while (!sc.hasNext("z")) {
char ch = sc.next().charAt(0);
System.out.print("[" + ch + "] ");
}
If you give this input:
123 a b c x y z
The output is:
[1] [2] [3] [a] [b] [c] [x] [y]
So what happens here is that the Scanner
uses \s*
as delimiter, which is the regex for "zero or more whitespace characters". This skips spaces etc in the input, so you only get non-whitespace characters, one at a time.

- 376,812
- 128
- 561
- 623
i found this way worked nice:
{
char [] a;
String temp;
Scanner keyboard = new Scanner(System.in);
System.out.println("please give the first integer :");
temp=keyboard.next();
a=temp.toCharArray();
}
you can also get individual one with String.charAt()

- 57
- 5
Here is a class 'getJ' with a static function 'chr()'. This function reads one char.
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class getJ {
static char chr()throws IOException{
BufferedReader bufferReader =new BufferedReader(new InputStreamReader(System.in));
return bufferReader.readLine().charAt(0);
}
}
In order to read a char use this:
anyFunc()throws IOException{
...
...
char c=getJ.chr();
}
Because of 'chr()' is static, you don't have to create 'getJ' by 'new' ; I mean you don't need to do:
getJ ob = new getJ;
c=ob.chr();
You should remember to add 'throws IOException' to the function's head. If it's impossible, use try / catch as follows:
anyFunc(){// if it's impossible to add 'throws IOException' here
...
try
{
char c=getJ.chr(); //reads a char into c
}
catch(IOException e)
{
System.out.println("IOException has been caught");
}
Credit to: tutorialspoint.com
See also: geeksforgeeks.

- 11
- 4
Maybe you could try this code:
import java.io.*;
public class Test
{
public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String userInput = in.readLine();
System.out.println("\n\nUser entered -> " + userInput);
}
catch(IOException e)
{
System.out.println("IOException has been caught");
}
}
}

- 87
- 2
- 12