Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!feeder1.xsusenet.com!feeder2.news.elisa.fi!feeder1.news.elisa.fi!uutiset.elisa.fi!7564ea0f!not-for-mail From: Jukka Lahtinen Newsgroups: comp.lang.java.programmer Subject: Re: Integer.parseInt() and whitespace? Organization: N/A References: <3f229a4e-af34-424f-b256-2e806e71015a@googlegroups.com> X-no-archive: yes Content-Transfer-Encoding: 8bit Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:EnHQhduwqxWYPM7H/PfDz+C9IZc= MIME-Version: 1.0 Content-Type: text/plain Lines: 21 Date: Sun, 05 Apr 2015 14:29:09 +0300 NNTP-Posting-Host: 91.155.88.191 X-Complaints-To: newsmaster@saunalahti.com X-Trace: uutiset.elisa.fi 1428233349 91.155.88.191 (Sun, 05 Apr 2015 14:29:09 EEST) NNTP-Posting-Date: Sun, 05 Apr 2015 14:29:09 EEST Xref: csiph.com comp.lang.java.programmer:31464 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