Help Integer.parseInt

Discussion in 'Programming General' started by Morphis, Jan 26, 2009.

Help Integer.parseInt
  1. Unread #1 - Jan 26, 2009 at 4:03 PM
  2. Morphis
    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0

    Morphis Apprentice
    Banned

    Help Integer.parseInt

    Well I am trying to convert a string to an int. The string grabs numbers from a website, and then i convert the string to an int that I can use to do calculations. My question is this, how can I convert a string with a comma to an int. I am getting an error when the number exceeds 3 digits and the website adds a comma.

    For example the website has a number - 1,487

    The string grabs that all good and it will return 1,487 but when i do
    Code:
    int x = Integer.parseInt(string); 
    I get an error because of the damn comma.

    Any help is greatly appreciated.
     
  3. Unread #2 - Jan 26, 2009 at 4:27 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Help Integer.parseInt

  5. Unread #3 - Jan 26, 2009 at 4:35 PM
  6. Morphis
    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0

    Morphis Apprentice
    Banned

    Help Integer.parseInt

    Nevermind, thanks I got it now I think. When I get home ill try this out
    Code:
    String newString = oldString.replace(",","");
    you think that will work?
     
  7. Unread #4 - Jan 26, 2009 at 6:06 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Help Integer.parseInt

    It should do the trick although I don't recall ever trying the replace function with a blank string ("") parameter.
     
  9. Unread #5 - Jan 26, 2009 at 11:13 PM
  10. Morphis
    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0

    Morphis Apprentice
    Banned

    Help Integer.parseInt

    Ya this is my first time even using it, and it works removes the commas, i later replace them with Number Formatting.
     
  11. Unread #6 - Jan 29, 2009 at 12:21 PM
  12. war833
    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0

    war833 Member

    Help Integer.parseInt

    If the replace function does not work then you could always convert it to a char array and then loop through it and shift everything left when the loop encounters a comma. Not the best solution but certainly an option should all others fail.
     
< A way to check who is viewing your profile on facebook. | Omg Please Just Help Real Quick~~~ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site