(Integer readFrom: 'abc' readStream) = 0

Damien Cassou damien.cassou at gmail.com
Tue Jan 8 16:11:38 UTC 2008


On Jan 8, 2008 3:57 PM, Zulq Alam <me at zulq.net> wrote:
> I was surprised to find that Integer class>>#readFrom: returns 0 when it
> doesn't find an integer. So, (Integer readFrom: 'abc' readStream) = 0 is


You should use #readExactlyFrom:


> true. This seems wrong to me although it's consistent with the method
> comment and VisualWorks. I was expecting an error. What's the reasoning
> behind this?
>
> I'm looking for the Smalltalk equivalent of:
>
> try {
>    Integer.parseInt("abc");
> }
> catch (NumberFormatException e) {
>    // do something
> }
>
> I found SqNumberParser which appears to be what I want:
>
> (SqNumberParser on: 'abc')
>         nextIntegerBase: 10
>         ifFail: [self doSomething]
>
> This works but, is it the right / good way?


Yes. SqNumberParser has been added in Squeak 3.9 and is the way to do
new number parsing.




-- 
Damien Cassou



More information about the Squeak-dev mailing list