Adblock breaks this site

Help Integer.parseInt

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

  1. Morphis

    Morphis Apprentice
    Banned

    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0
    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.
     
  2. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Help Integer.parseInt

  3. Morphis

    Morphis Apprentice
    Banned

    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0
    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?
     
  4. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Help Integer.parseInt

    It should do the trick although I don't recall ever trying the replace function with a blank string ("") parameter.
     
  5. Morphis

    Morphis Apprentice
    Banned

    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0
    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.
     
  6. war833

    war833 Member

    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0
    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~~~ >


 
 
Adblock breaks this site