A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
"I'm not back." - Bill Harding, Twister
Andrzej Lach wrote:I also got here from google. Good pattern but needs an update.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Your easiest solution would be to use Integer.parseInt() on the string and catch the exception if it is not a number.
Dennis Deems wrote:
Your easiest solution would be to use Integer.parseInt() on the string and catch the exception if it is not a number.
Lazy much? Exceptions are for telling you there is a problem in your application. Use the method posted by Campbell Ritchie seven years ago.
Mike Simmons wrote:
Dennis Deems wrote:
Your easiest solution would be to use Integer.parseInt() on the string and catch the exception if it is not a number.
Lazy much? Exceptions are for telling you there is a problem in your application. Use the method posted by Campbell Ritchie seven years ago.
What, we're insulting people for using parseInt() now? That's a perfectly valid solution unless performance is an issue. Stan James, RIP, discussed it above years ago, and he's still right. I'm also fond of the Scanner solution suggested above, also years ago.
Mike Simmons wrote:The religiously absolutist idea that exceptions can only be used to indicate errors in the application (as opposed to input data) can be pointlessly counterproductive, as not all libraries are actually designed this way.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:If this was a file as opposed specifically to user input, I doubt you would be advocating this style, so at best it works in situations where you know that it's the only way you're going to receive numbers.
Winston Gutkowski wrote:The advantage of a regex (or indeed Scanner) is that it can be applied to any InputStream, so if you're testing, you can change your mode of input; and if you wrap it in a decent utility method, who cares how complicated the actual code is?
Mike Simmons wrote:Well, I like regex and Scanner, but I don't see a big advantage here. For testing I'd just write a method that takes a String as input, and I can easily call it with any String input I want. Also, how do you apply a regex to an InputStream? The two ways I can think of are to use a Scanner, or read the InputStream into a String, neither of which sounds like what you're talking about. Maybe I'm overlooking a method here?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:You may have seen my rant a while back about methods that return booleans when in fact one that returns an index might make a better building block. Or indeed, provide both. (I lost BTW; at least in terms of votes )
Mike Simmons wrote:But I'd favor something like:...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:Sure, I was one of the main people arguing with you. Dennis was there too.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:I don't like cluttered APIs: Old fart; Alzheimers; too much to remember.
Winston Gutkowski wrote:BTW, I hope you don't take our butting heads from time to time personally. I really enjoy our little 'contretemps'; and I've learnt quite a lot from you. Not least, how not to be quite such an old fart.
Mike Simmons wrote:My preference for Scala's Option type is coloring my thinking here...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:Scala's approach is to create a data wrapper representing a nullable type like a container with 0 or 1 element...The syntax is weird at first, but very useful...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:Cool. However I was just looking over some Scala APIs; I suspect you won't be as thrilled with the plethora of arcanely-named methods available on all the collection-type classes.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Do you mean like the ATTACHED type in Eiffel?Winston Gutkowski wrote: . . . gimme some quick null checking. . . .
Mike Simmons wrote:And my dislike of returning null is strong enough that if someone wants to do it, I'd rather force them to do it explicitly (with parseInt(s, null)) rather than someone picking up that behavior accidentally and forget to check the return value.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |