JDK-5066699 : Add methods nextChar() and hasNextChar() to java.util.Scanner
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-06-22
  • Updated: 2016-06-06
  • Resolved: 2016-06-06
Name: jl125535			Date: 06/22/2004

I found out that the java.util.Scanner class is not orthogonal -- I mean, there is no method such as nextChar() or hasNextChar().  As I teach the Java classes, I think it is a bad idea for not having a full "primitive types coverage" in the Scanner class.

Therefore I would really love if the interface of Scanner class could be extended to contain the nextChar() and hasNextChar() methods.

As I teach the Java classes, I think it is a bad idea for not having a full "primitive types coverage" in the Scanner class.

For example, a student asked me once:
"How to read a real [double] value from the keyboard in a new way?"
I say:
"Use Scanner.nextDouble() method"

There goes another question:
"How to read a single chararcter from the keyboard in a new way?"
As I assumed that if there are methods such as nextInt(), nextDouble() and the like, I answered:
"Use Scanner.nextChar() method"

Ooops. There is no Scanner.nextChar() method... This way there is AGAIN an exception in Java libraries, which will be forever explained to the students:
* there is no nextChar() method
* use different approach
* every teacher will have different solution for this

The same applies to not having a hasNextChar() method.

I would like the interface of Scanner class to be extended to contain the nextChar() and hasNextChar() methods.
This way it would be perfect to write a program which reads a whole bunch of primitive values, as well as String values (look at the source code for an executable test case).

Currently there are no methods such as:
* java.util.Scanner.nextChar()
* java.util.Scanner.hasNextChar()

---------- BEGIN SOURCE ----------
import java.util.*;
import static java.lang.*;

public class InputTest {
    public static void main(String[] args) {
        Scanner sc = Scanner.create(System.in);

        out.print("Enter a line of text: ");
        String s = sc.nextLine();
        out.print("Enter an integer value: ");
        int i = sc.nextInt();
        out.print("Enter a double value: ");
        double d = sc.nextDouble();
        out.print("Enter single character: ");
        char c = sc.nextChar();    //  <---- There is no such (expected) method !

        out.printf("You entered:\n" +
            "[String] %s\n" +
            "[int]    %d\n" +
            "[double] %f\n" +
            "[char]   %c\n",
            s, i, d, c);
---------- END SOURCE ----------

// I'm not sure how to deal best with the above problem
(Incident Review ID: 280700) 
###@###.### 11/1/04 20:37 GMT

have not had any more similar request. closed as won't fix

EVALUATION The Scanner is intended for scanning input for particular patterns, not grabbing it a character at a time. If you want a single char at a time regardless of what it is, then use a Reader. If truly necessary a Scanner can do this by using findWithinHorizon with a pattern that accepts any single char. Even though I do not find this compelling I will leave this RFE open in case we receive a large number of requests for this. It is true that there is a certain lack of symmetry in the current method set. ###@###.### 2004-06-22