I have a question
how can I enter char
using Scanner in java?
Such as for integer number
Scanner input=new Scanner(System.in);
int a = input.nextInt();
but how can I do the same for char?
thanks for helping.
I have a question
how can I enter char
using Scanner in java?
Such as for integer number
Scanner input=new Scanner(System.in);
int a = input.nextInt();
but how can I do the same for char?
thanks for helping.
You could take the first character from Scanner.next:
char c = reader.next().charAt(0);
To consume exactly one character you could use:
char c = reader.findInLine(".").charAt(0);
To consume strictly one character you could use:
char c = reader.next(".").charAt(0);
You can do input.next(pattern) to get the next input which matches a certain pattern. If you set this to "." then it will just get the next character entered.
char c = input.next(".").charAt(0);
What you could do is use the Scanner
to take in the single character as a String
:
String s = input.next();
and then you could convert the String to a char like this:
char c = s.charAt(0);
then again, as @Elliott Frisch mentioned, you could just stop at the first line
If you need to read chars use InputStreamReader
Reader input=new InputStreamReader(System.in);
char c = (char)input.read();