- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Teamwork
- Cuts Like a Knife
- Charge Me
- Que Sera, Sera
- Hot Dog
- Sentinel Headline
- Mais Que Nada
- Here Comes the Sun
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It's a pity he did not fix the atrocious curly brace style while he was at it. This is java unworthy.
Admin
Does Java not have some form of the TryParseInt function?
Admin
This is the kind of method I re-read multiple times just to try to back into the thought process that produced it.
Edit Admin
"only to find 39 copies of this function, with minor variations." If only there were a way to reuse code in other parts of the project...
Edit Admin
No, Java doesn't have a tryParse built-in. There's just the parseInt used here that throws an exception (that Java forces a catch block for): https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html#parseInt(java.lang.String)
But it's a common thing to exist in utility libraries that many projects would be including anyway, like Apache Commons Lang: https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/math/NumberUtils.html#toInt-java.lang.String-
Of course, it might be implemented by just catching the exception of the built-in parseInt, so you're playing the Java exception performance penalty anyway for cases where it isn't a number: https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/math/NumberUtils.java#L1528