Results 1 to 14 of 14
  1. #1
    skiing is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default [SOLVED] String conversion

    I am trying to convert a string that is a float value to a float then add 25.00 to it and convert it back to a string. I have tried the following.

    String balance = rs.getString(9);

    float price = Integer.parseInt(balance);
    // val = price + 25;
    // String ic = Integer.toString(val)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    You use Integer to parse a String to an int not a float or double. Since Integer is the wrapper class for int, what class should you use to parse a String to float?

    Hint: Look in the API and you will find the class and your method.

    Also, you rarely want to use float when you can use the more accurate double type.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    You have made few mistakes there.

    Java Code:
            String str = "34.2";
            float val = Float.parseFloat(str);
            float newVal = val + 25.0F;
            str = String.valueOf(newVal);
            System.out.println(str);

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    OK, Eranga has given you the answer: to get a float from a String you need to use the Float class and parse it with a parseFloat method. I again recommend however that you not do this, but instead use Double.parseDouble(...)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    Yes, that's true. But there is a reason not to mentioned that in my post. Because our thread starter can confuse on float and double. Just look at his attempt. Seems no idea about wrapper classes, he's a newbie to Java. :)

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    12

    Default

    Quote Originally Posted by Fubarable View Post
    Also, you rarely want to use float when you can use the more accurate double type.
    For currency, using a double is not much more accurate than a float. And while the OP is new to Java, its never too soon to remember:

    Using a float or double for money/currency is a sin.

    Don't do it!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    fishtoprecords is absolutely correct. I didn't see that the OP was dealing with currency here.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    And if I'm remember correctly, fishtoprecords took a long discussion about that in one of his past thread. Sorry if I'm wrong.

  9. #9
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    12

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    I think it's better to look at our thread starter. May be even before he has to learn about basis as well.

  11. #11
    jhen is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default polymorphism

    can anyone give me a simple source code that demonstrates polymorphism in java?

    anyone plz:o

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    Quote Originally Posted by jhen View Post
    can anyone give me a simple source code that demonstrates polymorphism in java?

    anyone plz:o
    Please don't hijack another thread. if you have a question, post it in its own thread.

  13. #13
    skiing is offline Member
    Join Date
    Nov 2008
    Posts
    15
    Rep Power
    0

    Default Thanks

    It works now.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by jhen View Post
    can anyone give me a simple source code that demonstrates polymorphism in java?

    anyone plz:o
    Please don't deal with others thread, if your question is not related to. Start on separate thread in the correct sub-forum.

Similar Threads

  1. String to Integer Conversion in JSP
    By vinikz in forum New To Java
    Replies: 8
    Last Post: 11-10-2010, 02:45 PM
  2. String to Object conversion
    By moaxjlou in forum New To Java
    Replies: 1
    Last Post: 10-29-2008, 01:04 AM
  3. string conversion??
    By j2vdk in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:35 PM
  4. bytearray to string conversion
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 10:39 AM
  5. String to Date conversion
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •