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

java.util.Scanner.nextInt does not accept uppercase characters

XMLWordPrintable

    • b54
    • x86
    • linux



      Name: gm110360 Date: 05/19/2004


      FULL PRODUCT VERSION :
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      GNU/Linux 2.4.21-15.EL

      A DESCRIPTION OF THE PROBLEM :
      Scanner.getShort([radix]) (as well as getInt and getLong) does not accept uppercase characters (when radix>10). For example, the following fragment:

        Scanner sc = Scanner.create("ff 10 FF");
        System.out.println(sc.readInt(16));
        System.out.println(sc.readInt(16));
        System.out.println(sc.readInt(16));

      prints out 255, 16, then throws a InputMismatchException.

      In addition, once an InputMismatchException has been thrown, every subsequent attempt to throws the same exception even if the input is purely numeric

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the program below, then run (for example):

      java TestHexScanner 10 ff FF 1


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      16 255 255 1
      ACTUAL -
      16 255 java.util.InputMismatchException java.util.InputMismatchException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.util.InputMismatchException
              at java.util.Scanner.throwFor(Scanner.java:810)
              at java.util.Scanner.next(Scanner.java:1404)
              at java.util.Scanner.nextInt(Scanner.java:1996)
              .........

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class TestHexScanner{
      public static void main(String[] args){
      String s = "";
      for (String arg : args)
      s += arg + " ";
      Scanner sc = Scanner.create(s);
      for (int i=0; i<args.length; i++)
      try{
      System.out.print(" " + sc.nextInt(16));
      } catch(InputMismatchException e) {
      System.out.print(" " + e);
      }
      System.out.println("\n");
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Short.parseShort, Integer.parseInt and Long.parseLong still work fine, for example, the program above works fine if one uses

          Integer.parseInt(sc.next(), 16)

      instaed of

          sc.nextInt(16)
      (Incident Review ID: 270426)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: