我的程序拋出一個System.FormatException: "The entrystring has the wrong format."
每當我試圖運行此代碼時:System.Format.FormatException試圖double.Parse()
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;
}
我也試圖與Convert.ToDouble(elem)
和編碼的ASCII碼,UTF_8運行。這些都沒有奏效。
要理解我的代碼:
我調用該函數從另一個函數和CoordString
說法是這樣的: 90 10 1000
所以他們都是整數,但我需要他們的兩倍。 (我試過Int32.Parse()
,然後轉換爲加倍,在這裏它崩潰的Int32.Parse()
部分)
我的代碼應該得到CoordString("90 10 1000"
),並把它分割成單串(["90", "10", "1000"]
)。 Console.WriteLine(elem)
打印正確的數字,沒有字母,只是數字作爲字符串。
任何想法爲什麼/如何解決它?目前爲止沒有任何其他問題的建議。
編輯:
奇怪的是,打印elem
也工作得很好。但例外窗口告訴我這一點:
b 0 double
elem "" string
// The class name here
elem的值是多少? –
elem始終是來自'CoordString.Split('')'數組的每個單獨的字符串。它總是數字作爲字符串,所以首先它的'90',然後'10',然後'1000' –
我只是嘗試你的代碼,它工作得很好。你確定輸入字符串看起來像這個'「90 10 1000」'? –