🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: aus.computers.java
Article #302

Re: Help! String as Integer

#302
From: Ian Foster
Date: Tue, 16 Jan 2001 08:39
51 lines
2512 bytes
The problem you are having is that neither "foo" or "bar" are able to be
parsed as an integer, or put in another way "foo" and "bar" cannot be
represented as an integer.  The Intger.valueOf(String) method is only
valid if the Sting is a String representation of an integer.  Try this;

String a = "yes";
String b = "ls";
String aaString = "25";
String bbString = "937";
try {
    int aa = Integer.valueOf(aaString).intValue();
    int bb = Integer.valueOf(bbString).intValue();
}
catch (numberFormatException e) {
    System.out.printLn(e.getLocalizedMessage());
}
System.out.println(aa);
System.out.println(bb);

Regards,

Ian Foster

Luke Vanderfluit wrote:

> Hi
>
> I need help with this.
> Why doesn't the following code work?:
> I need to interpret a line of text as an integer.
>
> String a = "yes";
> String b = "ls";
> int aa = Integer.valueOf("foo").intValue();
> int bb = Integer.valueOf("bar").intValue();
> System.out.println(aa);
> System.out.println(bb);
>
> This throws a NumberFormatException
>
> How can I read a String as an Integer
>
> Can you help me please.
>
> Desperate,
> --
>   Luke Vanderfluit
>
>   Phone 61 8 8556 6112
>
>   Aldinga Beach, South Australia
Technical Details
Message-ID: <3A6408B8.E7337B30@fairclough.com.au>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!news-out.cwix.com!newsfeed.cwix.com!intgwpad.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mail
References: <3A63FC90.3C832EFA@chipcity.com.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a146,7de64e08ee658586
X-Google-Attributes: gid10a146,public
X-Google-Thread: 10d6db,7de64e08ee658586
X-Google-Attributes: gid10d6db,public
X-Google-Thread: 113ae9,7de64e08ee658586
X-Google-Attributes: gid113ae9,public
X-Google-ArrivalTime: 2001-01-16 00:34:05 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!news-out.cwix.com!newsfeed.cwix.com!intgwpad.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mail
Message-ID: <3A6408B8.E7337B30@fairclough.com.au>
From: Ian Foster <fcpl@fairclough.com.au>
X-Mailer: Mozilla 4.76 [en] (Windows NT 5.0; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: aus.computers.java,comp.lang.java.programmer,comp.lang.java.help
Subject: Re: Help! String as Integer
References: <3A63FC90.3C832EFA@chipcity.com.au>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 52
Date: Tue, 16 Jan 2001 16:39:20 +0800
NNTP-Posting-Host: 203.43.238.163
X-Trace: newsfeeds.bigpond.com 979633251 203.43.238.163 (Tue, 16 Jan 2001 19:20:51 EST)
NNTP-Posting-Date: Tue, 16 Jan 2001 19:20:51 EST
Organization: Telstra BigPond Internet Services (http://www.bigpond.com)