views:

521

answers:

5

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?

A: 

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
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.
VP
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
+1  A: 

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
+3  A: 

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
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
VP
A: 

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
A: 

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.

StarSignLeo
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
Yes, but as the substring is (0,1) this would always be the beginning and possible the end.
StarSignLeo