CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2002
    Location
    France
    Posts
    70

    converting a string to integer error

    I have this code
    temp = token.nextToken().toString();
    numero = Integer.parseInt(temp);
    Is is giving me a numberFormatExeption error Knowing that temp is a string and numero is declared to be an interger
    can anyone help

  2. #2
    Join Date
    Jan 2003
    Location
    India
    Posts
    52
    NumberFormatException indicates that string contains some non-numeric data, which converter is not able to convert.
    Which string are you trying to convert?

    Amol.

  3. #3
    Join Date
    Nov 2002
    Location
    France
    Posts
    70
    It is working now there was a space char before my string, i just saw it now, really it puzzeled me
    Thank for your reply

  4. #4
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    You might want to try and catch that exception.
    Code:
    int iNumber;
    String sNumber = " 23";
    
    try   {
       // trim the value and try to parse
       iNumber = Integer.parseInt(sNumber.trim());
    }catch(NumberFormatException nfe){
       // it's not a number
    }

    [goodz13: edited:Sorry I should have looked the responce over before I posted]
    Last edited by Goodz13; January 7th, 2003 at 02:20 PM.

  5. #5
    Join Date
    Nov 2002
    Location
    France
    Posts
    70
    Thanks Goodz13, you are right to be on the safe side i will be better off catching the exception

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured