Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Newbie? String to Integer

Thread view: 
David - 20 Mar 2004 17:46 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.