• Post Reply Bookmark Topic Watch Topic
  • New Topic

Should parseInt() be able to parse like "+2"?  RSS feed

 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote from java.lang.Integer.parseInt

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value.


I think this method should be able to parse string which the first character is an ASCII plus sign (example: "+2").

I want to hear others opinion.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm ambivalent. Positive numbers aren't normally written with a positive sign.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked in the API? I agree with David Newton that a + is not part of a number.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you looked in the API?

I'm guessing yes.

However, y'all might want to take a look at Float.parseFloat() and Double.parseDouble() as well. They do allow leading +. Just like javac does in Java source files:

It may be less common than omitting the +, but it is allowed.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a unary operator, just like minus, but we're talking about strings.

Numbers are positive without it, and I still don't have a problem with it not being accepted in a parse method, regardless of any inconsistencies in Sun's implementation.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have a problem with it being rejected by some parse methods, as it certainly isn't a common construct. But I'm also happy that there is some way to parse this using standard libraries, because it is a valid way to write a number, and it's a way that has been allowed in Java all along.

If it were up to me, I would have omitted this behavior from javac and the parseXXX() methods, but I would have put it in DecimalFormat, where they supposedly allow you to specify the format you want to use. Unfortunately, they didn't put this in DecimalFormat for some reason, so I guess putting it in parseXXX() is better than nothing. Would be nice to have all the custom options available in one place though.
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some other languages converts "+2" to 2

Javascript: parseInt("+2") => 2
Python: int("+2") => 2
C/C++: atoi("+2") => 2

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of all the bad things about Java, and all the API inconsistencies, this one is really, really, really low on my list.

File a bug report/request for enhancement if you have this much of an issue with it, or use a different conversion utility.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh... is there a problem with simply having a conversation about it?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh, no, who said there was? I don't understand what the point of the "conversation" is, but that's a different issue.

I suggested filing a bug/RFE because that's how one handles issues with Sun's APIs. I suggested using a different conversion methodology because that's how one works around perceived deficiencies in existing libraries. I stated my opinion that this is a pretty trivial inconsistency.

I didn't lock the thread or tell anybody they couldn't add their own opinion.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mm, I probably misinterpreted the tone of "if you have this much of an issue with it" then. Never mind. Cheers!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to be more careful with how I word things--text can be a tricky medium. Thanks for understanding.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!