My program throws a System.FormatException: "The entrystring has the wrong format."
whenever I am trying run this code:
public double[] ReturnCoordsFromString(string CoordString)
{
string[] cArrStr = CoordString.Split(' ');
List<double> NumList = new List<double>();
foreach (string elem in cArrStr)
{
Console.WriteLine(elem);
double b = Convert.ToDouble(elem); // <= Error is here
NumList.Add(b);
}
double[] retN = NumList.ToArray<double>();
return retN;
}
I have also tried to run it with Convert.ToDouble(elem)
and Encodings with ascii and utf_8. None of these worked.
To understand my code:
I call the function from another function and the CoordString
argument looks like this: 90 10 1000
So they are all Integers, but I need them as double. (I tried Int32.Parse()
and then convert to double, here it crashes on the Int32.Parse()
part)
My code should get the CoordString ( "90 10 1000"
) and split it into single strings ( ["90", "10", "1000"]
). The Console.WriteLine(elem)
prints the correct numbers, no letters, just numbers as string.
Any idea why / how to fix it? Nothing other questions suggested worked so far.
EDIT:
The weird thing is, printing elem
does work well. But the Exception Window shows me this:
b 0 double
elem "" string
// The class name here
You probably have a double space somewhere that's causing the problem. Try specifying StringSplitOptions.RemoveEmptyEntries
:
string[] cArrStr = CoordString(' ', StringSplitOptions.RemoveEmptyEntries)
instead of
string[] cArrStr = CoordString.Split(' ');
Also, you should use Double.TryParse
and not Convert.ToDouble
, since Double.TryParse
will only return false when it can't convert, while Convert.ToDouble
will throw an exception:
So use this:
double b;
if(Double.TryParse(elem, out d))
{
// value is a double
}
instead of
double b = Convert.ToDouble(elem);
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.