Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5066699

Add methods nextChar() and hasNextChar() to java.util.Scanner

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

      Name: jl125535 Date: 06/22/2004


      A DESCRIPTION OF THE REQUEST :
      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.

      JUSTIFICATION :
      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.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      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).

      ACTUAL -
      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 ----------

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

            sherman Xueming Shen
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: