Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

retarded Java question

Options
  • 26-03-2002 1:09am
    #1
    Registered Users Posts: 6,661 ✭✭✭


    Basically I need to convert strings from String objects into int and float objects.



    Probably should have learned this ages ago but.........how do you make an int out of a String? I'm writing a Servlet with some database stuff and can't seem to get the hang of it. Making a String out of an int is no problem - simple

    myint.toString();
    or
    myString = valueOf(myInt);

    Doing it the other way around is giving me grief. Couldn't find anything in my Java book about it, but the tutorials, etc on the Java site said use valueOf() but isn't really clear on how to use it. Both :

    myint = valueOf(myString);
    and
    myint.valueOf(myString);

    produce errors. The Java site also says use :

    myInteger = new Integer(Integer.parseInt(myString));
    or do it using the constructer :
    Integer myInteger = new Integer(myString);

    Both seem to work (sort of) but of course an Integer isn't an int, and any comparrissons I try crash the servlet. Using either :

    myInteger == myOtherInteger
    or
    myint = myInteger.compareTo(myOtherInteger);







    Basically I need to convert strings from String objects into int and float objects.


Comments

  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    I always say that, and people laugh :(

    Anyway :cool:

    Todays lesson revolves arounds something called wrapper classes. Since primative datatypes such as int/float/double etc are not actually objects as in they don't inherit from java.lang.Object, you need something called a wrapper class to make them objects. In your case you need the Integer object as oposed to just plain 'ol int.

    What you do is this:


    String number = new String("1234");
    int value=0;

    Integer i = new Integer(number);

    value = i.intValue();


    value is a primitive int datatype that now contains the number you want.

    If you wanted to convert to a double or float, thankfully the guys @ Sun have done it again, and created Float and Double wrapper classes for us to play with.

    ;-phobos-)


  • Registered Users Posts: 1,931 ✭✭✭Zab


    Going along with what phobos has said, the Integer and Float classes have static methods called parseInt() and parseFloat() respectively.

    Both of these methods return primative datatypes, so you can use them directly without ever creating an instance of the class.

    ie.
    int i = Integer.parseInt( "1234" );
    float f = Float.parseFloat( "12.34" );

    Zab.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    wouldn't number also have a method like .toInteger()?


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    hey where do u Java gurus work? im doing advance Java course now, and il be looking for work in about 3 months hopefully in java, im sick of vb and c++!!!


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Originally posted by Morphéus
    hey where do u Java gurus work? im doing advance Java course now, and il be looking for work in about 3 months hopefully in java, im sick of vb and c++!!!

    wow my sarcasm meter exploded! :)

    Actually I was getting confused with something else. I checked and say zab is correct.

    btw, I'm not a java guru :p currently studying.. (or trying to get time to) for the J2 certification, again not for work more for personal.

    I'll be happy to answer any questions about using java in Domino though. :p

    btw, what is advanced java?


  • Advertisement
  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    wouldn't number also have a method like .toInteger()?

    You'd think it would, wouldn't you!

    I remember when I first ran in to that wall, if was a matter of fluke that I came across these wrapper guys. Thankfully Sun think of everything.

    ;-phobos-)


  • Registered Users Posts: 6,661 ✭✭✭Blitzkrieger


    See how simple it looks when you know how? :)

    Thanks guys.


Advertisement