Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31460 > unrolled thread

Integer.parseInt() and whitespace?

Started bygalt_57@hotmail.com
First post2015-04-04 20:16 -0700
Last post2015-04-05 13:22 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Integer.parseInt() and whitespace? galt_57@hotmail.com - 2015-04-04 20:16 -0700
    Re: Integer.parseInt() and whitespace? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2015-04-05 14:29 +0300
    Re: Integer.parseInt() and whitespace? Arne Vajhøj <arne@vajhoej.dk> - 2015-04-05 13:22 -0400

#31460 — Integer.parseInt() and whitespace?

Fromgalt_57@hotmail.com
Date2015-04-04 20:16 -0700
SubjectInteger.parseInt() and whitespace?
Message-ID<3f229a4e-af34-424f-b256-2e806e71015a@googlegroups.com>
So Integer.parseInt(str) fails if the string contains whitespace?

[toc] | [next] | [standalone]


#31464

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2015-04-05 14:29 +0300
Message-ID<lvpp7iyfsq.fsf@saunalahti.fi>
In reply to#31460
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> galt_57@hotmail.com writes:
>>So Integer.parseInt(str) fails if the string contains whitespace?

>   In that case java.lang.Integer.parseInt(java.lang.String)
>   behaves according to its specification and throws a
>   NumberFormatException when a conforming implementation of

In many cases, it is a good idea to first use the trim() method to the
String to be parsed.

int number;
String s = getStringToBeParsed(); // whatever you do to get the input
if (s != null) {
    number = Integer.parseInt(s.trim());
} else {
    // what ever is the proper thing to do in the particular case
}

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#31469

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-04-05 13:22 -0400
Message-ID<55216f73$0$286$14726298@news.sunsite.dk>
In reply to#31460
On 4/4/2015 11:16 PM, galt_57@hotmail.com wrote:
> So Integer.parseInt(str) fails if the string contains whitespace?

https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-

states:

<quote>
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 or an ASCII plus sign '+' ('\u002B') to indicate a 
positive value.
</quote>

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web