The following code is my test harness for both correctness and performance. Based on this example I don't see the reason for advocating the use of Scanner. I'm open to suggestions.
---=== C O D E A B B E Y ===--- my site with programming problems (and Certificates)
Rodion Gork wrote:However, consider that you maintain the code and you need to change your isInteger to isReal. With either approach except the regexp this will not take a lot of time from you. Probably few seconds to change "nextInt" to "nextDouble".
But if you try to extend your pattern, you may find it is more tricky business - and your team-lead will not allow you to merge your code without unit-test to check that your pattern is correct... And then someone should surely code-review both pattern and its unit-test... I believe this can take significant time from two persons...
![]()
Some nasty people will even ask you to write tests against this simple pattern for integer, I believe...)
Campbell Ritchie wrote:You will find a mention of how Scanner determines whether the String is an int here, from two weeks ago. It seems to use some regex then a try.
Carey Brown wrote:In creating the regex for type double I had to permit leading zeros whereas the regex's found on the internet did not.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
My goal is a regex that permits strings that would process properly by Double.parseDouble(), not counting range checking. parseDouble() not only permits leading zeros but it permits leading and trailing white space, which parseInt() does not.Winston Gutkowski wrote:
Carey Brown wrote:In creating the regex for type double I had to permit leading zeros whereas the regex's found on the internet did not.
I'm not quite sure I agree with that statement. Your program may have to handle leading 0's, but there's no particular reason your regex has to, especially if you remove them before you pass the String to it.
It's actually a common failing of many regexes - they try to do too much in one pass, and in the process become overly complex.
My 2¢.
Winston
Carey Brown wrote:My goal is a regex that permits strings that would process properly by Double.parseDouble(), not counting range checking. parseDouble() not only permits leading zeros but it permits leading and trailing white space, which parseInt() does not.
A typical regex found on the internet starts out with: "[-+]?(0|([1-9][0-9]*))
To allow leading zeros all I need is: "[-+]?\\d+
...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
But it is my personal opinion - albeit with a few grey hairs of experience. YMMV. :-)
I hadn't been here for fifteen years
A.J. Côté wrote:Scanner is slow. It is just a level of abstraction above parse and throw.
To insure Java language consistency, We need to validate that an int is an int, a double is a double, etc. in a central point; that is the parse() and throw concept. All higher level abstraction layers use it under the cover.
In the end, it is always faster to use your own BufferedReader than a scanner. Let me know if you need more background. I will provide links to other threads on JavaRanch.
Then again, don't optimize too soon ;-))
Carey Brown wrote:I was on board with Java before Scanner came into existence and I was just trying to get a sense of why people seem to promote it so heavily on this site....
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paul Clapham wrote:Or if you aren't a fan of naked nulls, you could use this:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:I have a copy of Urma Fusco and Mycroft's Java8 book...If you can get a copy it is worth a read
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
y final regex looks like:
...
Works like a charm and I don't feel like it's overly complex when broken down this way.
---=== C O D E A B B E Y ===--- my site with programming problems (and Certificates)
I did too, for 11 years (Belgium).Rodion Gork wrote:Now let us think. I live in such a strange country where people use comma instead of decimal dot (as a programmer I hate even thinking of this).
Very good point. I'm not absolutely sure, but I think that Double.valueOf() takes Locale into account as well, but I wouldn't swear to it.Scanner respects this bewildering tradition if proper locale is chosen. Suppose, your code is added to some internationally-targeted web-application and your QA tells you about this mournful fact - will you insist on trying to add localization to regexp, or ... switch to Scanner?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here