This is really a curiosity more than a problem...
Why doesn't the Scanner
class have a nextChar()
method? It seems like it should when you consider the fact that it has next
, nextInt
, nextLine
etc method.
I realize you can simply do the following:
userChar = in.next().charAt(0); System.out.println( userChar );
But why not have a nextChar()
method?
nextChar() scanner class in JavaThere is no classical nextChar() method in Java Scanner class. The best and most simple alternative to taking char input in Java would be the next(). charAt(0).
close() method closes this scanner. If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked.
You can cancel active requests for input either by interrupting the thread, or by calling cancel(), which canceles the currently waiting request. It uses Semaphores and threads to create a blocking nextLine() method that can be interrupted/canceled elsewhere.
In Java, the NextChar() and Next() operate and return consequent token/word within the input as a string and charAt() the first returns the primary character in that string.
The reason is that the Scanner class is designed for reading in whitespace-separated tokens. It's a convenience class that wraps an underlying input stream. Before scanner all you could do was read in single bytes, and that's a big pain if you want to read words or lines. With Scanner you pass in System.in, and it does a number of read() operations to tokenize the input for you. Reading a single character is a more basic operation. Source
You can use (char) System.in.read();
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With