10

Possible Duplicate:
how can I convert String to Int ?

Hi,

I have the following problem converting string to an integer:

string str = line.Substring(0,1);

//This picks an integer at offset 0 from string 'line' 

So now string str contains a single integer in it. I am doing the following:

int i = Convert.ToInt32(str);

i should be printing an integer if I write the following statement right?

Console.WriteLine(i);

It compiles without any error but gives the following error on runtime:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

Any help please?

Community
  • 1
  • 1
zack
  • 7,115
  • 14
  • 53
  • 63
  • Tag this with the language you are using. – sal Aug 20 '09 at 04:26
  • If you can provide the string value inside line variable it could be much more easier to detect the error. – rahul Aug 20 '09 at 04:31
  • This is a duplicate many times over, please see http://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int, http://stackoverflow.com/questions/952469/convert-string-to-int, http://stackoverflow.com/questions/887586/convert-string-to-int. – Andrew Hare Aug 20 '09 at 04:51

5 Answers5

19

Rather than using Convert.ToInt32(string) you should consider using Int32.TryParse(string, out int) instead. The TryParse methods are there to help deal with user-provided input in a safer manner. The most likely cause of your error is that the substring you are returning has an invalid string representation of an integer value.

string str = line.Substring(0,1);
int i = -1;
if (Int32.TryParse(str, out i))
{
   Console.WriteLine(i);
}
Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
  • thanks for the piece of code..I will try it out tomorrow as I do not have access to my workstation right now..will update the output here tomorrow..whatever it may be..thanks again – zack Aug 20 '09 at 04:54
  • What's the reasoning for setting i to -1 in the supplied example? It suggests to me that after a failed parse you could test for -1 in some manner but TryParse sets i to 0 if the parse fails – John Lewin Jan 28 '11 at 15:31
1

FormatException

value does not consist of an optional sign followed by a sequence of digits (0 through 9).

The exception that is thrown when the format of an argument does not meet the parameter specifications of the invoked method.

You can use Int32.TryParse if you don't want to generate an exception like this.

Int32.TryParse: Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

rahul
  • 184,426
  • 49
  • 232
  • 263
0

It's entirely possible there is some whitespace in there. Try running something akin to trim() (I'm not sure what language you're in) that will strip the white space. Also, try printing out the string to make sure you actually have the right part of it. We've all done that :)

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • Well, when I output the string on the Console using Console.WriteLine(str) it prints out a symbol (a smiley basically) and since I give the count as 1 I dont think there can be any other character except for the integer (which is getting printed out as a smiley). I am using C# in visual Studio 2005 by the way. – zack Aug 20 '09 at 04:32
  • 1
    If the console can't print it as any integer, then I'm guessing it isn't an integer. I could be wrong here, but my understanding of this method is to convert a string representation of an integer, say "1234" into the integer-datatype representation of 1234, eg something you could do math with. Is that what you're trying to do? – Chris Thompson Aug 20 '09 at 04:45
0

It's likely that your input is not a valid format. Try this instead. If the number is not valid, it should output an error.

Keep in mind that the string should consist of an optional sign followed by a number.

string line = "23"; // or whatever.
string str = line.Substring(0,1);
int i = 0;
if (Int32.TryParse(str, out i)) {
   Console.WriteLine(i);
} else {
    Console.WriteLine ("Error converting '" + line + "', '" + str + "'.");
}

One thing you may be seeing is the user entering "-1" for example. If you do the substring(0,1) on that, you'll only get "-" which isn't really valid.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Are you sure that the value returned in str is an int, set a debug point if your using visual studio. Ive got a feeling your problem maybe that your not actually returning an integer. Try:

line.Trim().Substring(0,1);

This will remove any whitespace.

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
StarSignLeo
  • 256
  • 1
  • 2
  • 9
  • Trim() will only remove whitespace from the end and beginning of the string, so this isn't guaranteed to solve the problem depending on what the original string contains. – Scott Dorman Aug 20 '09 at 05:02
  • Yes, but as the substring is (0,1) this would always be the beginning and possible the end. – StarSignLeo Aug 20 '09 at 05:18