Hello All,

When I'm trying to implement Scanner.nextInt(int radix), I met a problem.

As we all know, Character.MIN_RADIX equals 2 and Character.MAX_RADIX equals 36, so the parameter radix can not less than 2 or greater than 36, Otherwise that parameter is illegal.

But on RI, when the parameter radix is illegal, there are different kinds of Exception thrown.

   * If the parameter is less than 0 or greater than 36, RI throws a
     StringIndexOutOfBoundsException. Obviously this exception depends
     an RI's implementation.
   * If the parameter equals 1, RI throws an InputMismatchException
     with an additional information "The radix 1 is less than the
     Character.MIN_RADIX".
   * If the parameter equals 0, RI throws a
     java.util.regex.PatternSyntaxException whose constructor has three
     parameters. And the parameters depends on RI's implementation,
     that makes me can not follow RI.

And nothing is documented in the spec. Shall I follow RI's behavior? Thanks a lot.

Here is the test case to demo this issue.

public void test_nextIntI(){
      Scanner s = new Scanner("123 456");

      try {
           s.nextInt(-1);
           fail("Should throw StringIndexOutOfBoundsException");
       } catch (StringIndexOutOfBoundsException e) {
           // Expected
       }
       try {
           s.nextInt(0);
           fail("Should throw PatternSyntaxException");
       } catch (PatternSyntaxException e) {
           // Expected
       }
       try {
           s.nextInt(1);
           fail("Should throw InputMismatchException");
       } catch (InputMismatchException e) {
           // Expected
       }
       try {
           s.nextInt(40);
           fail("Should throw StringIndexOutOfBoundsException");
       } catch (StringIndexOutOfBoundsException e) {
           // Expected
       }
}

--
Richard Liang
China Software Development Lab, IBM

Reply via email to