I have a list of Characters and I want to add each character to the list as a user enters them (keyboard is a Scanner). I've tried two different approaches, neither works:
char temp = keyboard.nextByte();
charList.add(temp);
or
charList.add(keyboard.nextBye();
So how would I go about this?
If you want to just accept a char input
char temp keyboard.next().charAt(0);
charList.add(temp);
Always use the second, since you don't have any idea that user may enter String. Hence the second works for both.
us this Scanner reader = new Scanner(System.in);
char c = reader.nextChar();
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.