I have one problem with convert word to int. Is it badly written?
foreach (string line in lines)
{
string[] words = line.Split(' ');
foreach (string word in words)
{
{
tab[i] = Int32.Parse(word);
Console.WriteLine(tab[i]);
i++;
}
}
}
On these line:
tab[i] = Int32.Parse(word);
I have error:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
file:
0
12 0
19 15 0
31 37 50 0
22 21 36 20 0
17 28 35 21 25 0
Your string doesn't represent a clean int. Probably a number with decimal places? (eg 5.5?) To catch and print the word you can just use a try/catch.
This means there's something wrong with your input. Consider using Int.TryParse, which will work assuming you're only looking to use well-formed integers. You could also rig up some output on failure to give you an idea of what values are failing to parse, for example,
bool b;
int tempNumber;
foreach (string line in lines)
{
string[] words = line.Split(' ');
foreach (string word in words)
{
b = Int32.TryParse(word, out tempNumber);
if (b) // success
{
tab[i] = tempNumber;
Console.WriteLine(tab[i]);
i++;
}
else // handle error
{
Console.WriteLine("Error: '" + word + "' could not be parsed as an Int32");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.