🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: aus.computers.java
11 messages
11 total messages Started by Luke Vanderfluit Tue, 16 Jan 2001 07:47
Help! String as Integer
#300
Author: Luke Vanderfluit
Date: Tue, 16 Jan 2001 07:47
26 lines
1770 bytes
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
Re: Help! String as Integer
#301
Author: mitchell
Date: Tue, 16 Jan 2001 07:57
50 lines
2222 bytes
 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);




try that








Luke Vanderfluit <luke@chipcity.com.au> wrote in message
news:3A63FC90.3C832EFA@chipcity.com.au...
> 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
>
>
>
Re: Help! String as Integer
#302
Author: 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
Re: Help! String as Integer
#303
Author: Paul Murray
Date: Thu, 18 Jan 2001 00:16
19 lines
1868 bytes
"Luke Vanderfluit" <luke@chipcity.com.au> wrote in message
news:3A63FC90.3C832EFA@chipcity.com.au...

> Why doesn't the following code work?:
>
> 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

The integer value of "1" is 1. The integer value of "2" is 2. What do you
imagine the integer value of "foo" is? "foo" is not a number.

If you need to parse text values as numbers, maybe you need to look at
java.text.ChoiceFormat
Re: Help! String as Integer
#314
Author: rastgoo
Date: Thu, 31 May 2001 21:02
32 lines
2015 bytes
Luke Vanderfluit <luke@chipcity.com.au> wrote in message
news:3A63FC90.3C832EFA@chipcity.com.au...
> 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
>
>
>
Re: Help! String as Integer
#315
Author: Chris Smith
Date: Thu, 31 May 2001 22:55
21 lines
1672 bytes
> Luke Vanderfluit <luke@chipcity.com.au> wrote ...
> > I need to interpret a line of text as an integer.
> >

Using what conversion?

> > 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

As well it should, since neither "foo" nor "bar" are valid integer values.
What integer value do you want to get back?  For one possible integer value,
you could use "foo".hashCode(), but that may not be what you want.  What
*do* you want?

Chris Smith
Re: Help! String as Integer
#316
Author: Carl Schodde
Date: Fri, 01 Jun 2001 01:11
27 lines
2151 bytes
"Chris Smith" <cdsmith@twu.net> wrote in message
news:9f6i6i$h5d$0@dosa.alt.net...
> > Luke Vanderfluit <luke@chipcity.com.au> wrote ...
> > > I need to interpret a line of text as an integer.
> > >
>
> Using what conversion?
>
> > > 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
>
> As well it should, since neither "foo" nor "bar" are valid integer values.
> What integer value do you want to get back?  For one possible integer
value,
> you could use "foo".hashCode(), but that may not be what you want.

You could also use Integer.parseInt("foo");


> What *do* you want?
Re: Help! String as Integer
#317
Author: Chris Smith
Date: Fri, 01 Jun 2001 01:22
10 lines
1434 bytes
"Carl Schodde" <schodde@ozemail.com.au> wrote ...
> You could also use Integer.parseInt("foo");
>

Well, the point of my post is that you can't.  The OP obviously want some
specific form of unusual generation of an integer value from text that it's
integral in nature.  The above will throw an exception, not convert a string
to an integer.

Chris Smith
Re: Help! String as Integer
#318
Author: Paul Murray
Date: Fri, 01 Jun 2001 15:05
46 lines
2429 bytes
See java.text.ChoiceFormat .

--
--------------------------------------------------------------
Sometimes he answers "Yes", sometimes "No", sometimes "Wait", and sometimes
he gives you something different.
You get the same set of responses praying to a rock.
http://www.users.bigpond.com/pmurray
ICQ: 26066755

"rastgoo" <rastgoo@thefreeinternet.co.uk.NOSPAM> wrote in message
news:3b16b1bd_3@news.bizonline.net...
>
> Luke Vanderfluit <luke@chipcity.com.au> wrote in message
> news:3A63FC90.3C832EFA@chipcity.com.au...
> > 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
> >
> >
> >
>
>
Re: Help! String as Integer
#323
Author: kelb
Date: Mon, 04 Jun 2001 20:08
74 lines
2942 bytes
try this:

String bb;

int xx = 2;

bb = String.valueOf(xx);

this will give the value of the xx var as a String object...

--this valueOf(int x) method is static so you don't need to instantiate a
String object to utilize it...

or, if you're trying to convert an int to a String, just use this:

Integer xx = new Integer(2);

xx.toString();

best.

kelbarnes.

"Paul Murray" <pmurray@nospam.bigpond.com> wrote in message
news:NEYR6.31881$hV3.53039@newsfeeds.bigpond.com...
> See java.text.ChoiceFormat .
>
> --
> --------------------------------------------------------------
> Sometimes he answers "Yes", sometimes "No", sometimes "Wait", and
sometimes
> he gives you something different.
> You get the same set of responses praying to a rock.
> http://www.users.bigpond.com/pmurray
> ICQ: 26066755
>
> "rastgoo" <rastgoo@thefreeinternet.co.uk.NOSPAM> wrote in message
> news:3b16b1bd_3@news.bizonline.net...
> >
> > Luke Vanderfluit <luke@chipcity.com.au> wrote in message
> > news:3A63FC90.3C832EFA@chipcity.com.au...
> > > 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
> > >
> > >
> > >
> >
> >
>
>
Re: Help! String as Integer
#324
Author: Jon Skeet
Date: Mon, 04 Jun 2001 20:21
31 lines
2006 bytes
kelb <bkkelly@qwest.net> wrote:
> try this:
> 
> String bb;
> 
> int xx = 2;
> 
> bb = String.valueOf(xx);
> 
> this will give the value of the xx var as a String object...
> 
> --this valueOf(int x) method is static so you don't need to instantiate a
> String object to utilize it...
> 
> or, if you're trying to convert an int to a String, just use this:
> 
> Integer xx = new Integer(2);
> 
> xx.toString();

You seem to be presenting them as solutions to different problems, when 
actually they both solve *exactly* the same thing.

The second method, however, creates an Integer object unnecessarily.

I prefer String.valueOf (xx); myself.

-- 
Jon Skeet - skeet@pobox.com
http://www.pobox.com/~skeet
If replying to the group, please don't mail me at the same time
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads