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


Groups > comp.lang.java.programmer > #31464

Re: Integer.parseInt() and whitespace?

From Jukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Integer.parseInt() and whitespace?
Organization N/A
References <3f229a4e-af34-424f-b256-2e806e71015a@googlegroups.com> <whitespace-20150405052154@ram.dialup.fu-berlin.de>
Message-ID <lvpp7iyfsq.fsf@saunalahti.fi> (permalink)
Date 2015-04-05 14:29 +0300

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web