Richard Liang wrote:
Paulex Yang wrote:
Richard Liang 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.
Is this exception thrown by RegEx class? Should we identify at first
if our regular expression has different behavior with RI on some
certain input parameter?
Hello Paulex,
Yes, this exception is thrown by regex, but it depends on RI's
implementation. I don't know how to get the "input parameter", any
comments?
Oops, so just forget what I said:). Sorry if I made confusion, I didn't
look closely, just wondering why the regex exception is thrown from
Scanner, and I thought it may be straightforward to separately test the
regex API invocation, probably I'm wrong.
Richard
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
}
}
--
Paulex Yang
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]