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 = reade
There are three ways to approach this problem:
Call next()
on the Scanner, and extract the first character of the String (e.g. charAt(0)
) If you want to read the rest of the line as characters, iterate over the remaining characters in the String. Other answers have this code.
Use setDelimiter("")
to set the delimiter to an empty string. This will cause next()
to tokenize into strings that are exactly one character long. So then you can repeatedly call next().charAt(0)
to iterate the characters. You can then set the delimiter to its original value and resume scanning in the normal way!
Use the Reader API instead of the Scanner API. The Reader.read()
method delivers a single character read from the input stream. For example:
Reader reader = new InputStreamReader(System.in);
int ch = reader.read();
if (ch != -1) { // check for EOF
// we have a character ...
}
When you read from the console via System.in
, the input is typically buffered by the operating system, and only "released" to the application when the user types ENTER. So if you intend your application to respond to individual keyboard strokes, this is not going to work. You would need to do some OS-specific native code stuff to turn off or work around line-buffering for console at the OS level.
Reference:
This actually doesn't work:
char c = reader.next().charAt(0);
There are some good explanations and references in this question: Why doesn't the Scanner class have a nextChar method? "A Scanner breaks its input into tokens using a delimiter pattern", which is pretty open ended. For example when using this
c = lineScanner.next().charAt(0);
for this line of input "(1 + 9) / (3 - 1) + 6 - 2" the call to next returns "(1", c will be set to '(', and you'll end up losing the '1' on the next call to next()
Typically when you want to get a character you would like to ignore whitespace. This worked for me:
c = lineScanner.findInLine("[^\\s]").charAt(0);
Reference: regex to match a single character that is anything but a space
import java.util.Scanner;
public class userInput{
public static void main(String[] args){
// Creating your scanner with name kb as for keyBoard
Scanner kb = new Scanner(System.in);
String name;
int age;
char bloodGroup;
float height;
// Accepting Inputs from user
System.out.println("Enter Your Name");
name = kb.nextLine(); // for entire line of String including spaces
System.out.println("Enter Your Age");
age = kb.nextInt(); // for taking Int
System.out.println("Enter Your BloodGroup : A/B/O only");
bloodGroup = kb.next().charAt(0); // For character at position 0
System.out.println("Enter Your Height in Meters");
height = kb.nextFloat(); // for taking Float value
// closing your scanner object
kb.close();
// Outputting All
System.out.println("Name : " +name);
System.out.println("Age : " +age);
System.out.println("BloodGroup : " +bloodGroup);
System.out.println("Height : " +height+" m");
}
}
You could use typecasting:
Scanner sc= new Scanner(System.in);
char a=(char) sc.next();
This way you will take input in String due to the function 'next()' but then it will be converted into character due to the 'char' mentioned in the brackets.
This method of conversion of data type by mentioning the destination data type in brackets is called typecating. It works for me, I hope it works for u :)
package Pack;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
char c = reader.next(".").charAt(0);
}
}
To get only one character char c = reader.next(".").charAt(0);
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);