I am trying to find a way to take a char
input from the keyboard.
I tried using:
Scanner reader = new Scanner(System.in);
char c = reader.nextChar();
This method doesn't exist.
I tried taking c
as a String
. Yet, it would not always work in every case, since the other method I am calling from my method requires a char
as an input. Therefore I have to find a way to explicitly take a char as an input.
Any help?
You should get the String using scanner.next() and invoke String.charAt(0) method on the returned String.
Exmple :
output
By the way, you can't take a char directly as an input. As you can see above, a String is first taken then the charAt(0); is found and stored
To find the index of a character in a given sting, you can use this code:
You could take the first character from
Scanner.next
:To consume exactly one character you could use:
To consume strictly one character you could use: