Hi Richard,

Very good example.
You are right, spec says nothing about invalid radix processing for
nextInt(). RI behavior just proves they have no guard check. However
useRadix() produces IAE for the invalid radix and the sound of logic
says that IAE is a proper reaction for wrong radix in any method with
radix. Looks like a bug in RI for me...

Don't beat me please :-), but I would suggest two approaches:
- either no checks for radix, so that underlying algorithm does or
doesn't produce some exceptions
- implement guard check and produce IAE.

I believe that right solution is the second one - produce IAE -
however it potentailly results in less compatibility with RI.

--
Anton Avtamonov,
Intel Middleware Products Division

On 7/6/06, Richard Liang <[EMAIL PROTECTED]> wrote:
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




---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to