Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three ways to approach this problem:</p> <ul> <li><p>Call <code>next()</code> on the Scanner, and extract the first character of the String (e.g. <code>charAt(0)</code>) 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.</p></li> <li><p>Use <code>setDelimiter("")</code> to set the delimiter to an empty string. This will cause <code>next()</code> to tokenize into strings that are exactly one character long. So then you can <em>repeatedly</em> call <code>next().charAt(0)</code> to iterate the characters. You can then set the delimiter to its original value and resume scanning in the normal way!</p></li> <li><p>Use the Reader API instead of the Scanner API. The <code>Reader.read()</code> method delivers a single character read from the input stream. For example:</p> <pre><code>Reader reader = new InputStreamReader(System.in); int ch = reader.read(); if (ch != -1) { // check for EOF // we have a character ... } </code></pre></li> </ul> <hr> <p>When you read from the console via <code>System.in</code>, 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.</p> <p>Reference:</p> <ul> <li><a href="https://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it">How to read a single char from the console in Java (as the user types it)?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload