Ok very basic question how do i convert a string to an integer object?
I have tried the following:
String thePrice = request.getParameter("bidprice");
Integer comparePrice = new Integer(0);
comparePrice.parseInt(thePrice);
But when i print it out i get zero for comparePrice, where am i going wrong?
Many thanks Dave
Andrew Harker - 20 Mar 2004 18:29 GMT
> Ok very basic question how do i convert a string to an integer object?
Do you want an 'Integer' object or an 'int'? Use the
static methods of Integer (no need to create an instance
to do the parsing)
String s = "53";
int n = Integer.parseInt(s);
Integer i = Integer.valueOf(s);
> I have tried the following:
>
> String thePrice = request.getParameter("bidprice");
> Integer comparePrice = new Integer(0);
Here you have set the value of the Integer to
zero. Integer's are immutable so you cannot change
them although you could alter the ref. they point to.
> comparePrice.parseInt(thePrice);
>
> But when i print it out i get zero for comparePrice, where am i going wrong?
You set it to zero :-)
> Many thanks Dave
Virgil Green - 21 Mar 2004 07:03 GMT
> Ok very basic question how do i convert a string to an integer object?
>
[quoted text clipped - 3 lines]
> Integer comparePrice = new Integer(0);
> comparePrice.parseInt(thePrice);
In addition to Andrew's comments, note that this line of code is using an
object reference to invoke a static method of the Integer class. That is
considered bad form. It makes you think you are doing something to/with the
object you are referencing. parseInt() returns a value, it doesn't alter an
object on which it invoked. As Andrew noted, you don't even need your
Integer object. You merely need Integer.parseInt(thePrice) and need to
assign its output to some variable.
- Virgil
Bryce (Work) - 22 Mar 2004 15:00 GMT
>Ok very basic question how do i convert a string to an integer object?
>
[quoted text clipped - 5 lines]
>
>But when i print it out i get zero for comparePrice, where am i going wrong?
Print out thePrice string and see what it is. Maybe you aren't getting
the parameter?
Also, try this:
Integer comparePrice = Integer.valueOf(thePrice);
A little less wordy.
--
now with more cowbell