GPS数据解析和数组异常 [英] GPS data parsing and array exception
问题描述
亲爱的大家,
我最近正在为GPS数据解析编程.
GPS数据格式如下:
Dear guys,
I was recently programming for GPS data parsing.
GPS data format is as follows:
$GNGGA,152732.60,5257.1205318,N,00111.0161931,W,4,12,0.8,90.514,M,,,0.91,0011*11
我想逐行读取数据并在字符串类中使用Split方法提取纬度(字符"N"之前的数字).我使用了以下代码,
I want to read the data line by line and use the Split method in string class to extract the latitude(the number before character ''N''). I used the following code,
string line;
line = sr.ReadLine();
while ((line = sr.ReadLine()) != null)
{
string[] temp = line.Split(',');
Console.WriteLine(temp[2]);
}
但是我在编译器中发现了以下错误.
but i found the following errors by the compiler.
Index was outside the bounds of the array.
我通过阅读数组教程来进行一天的研究,但我仍然对此感到困扰.
有人知道我的代码和可用来提取信息并将其保存在数组中并逐行输出到screan上的解决方案的解决方案吗?非常感谢您的帮助.
I work on it for one day by reading array tutorial,but i still bother with it.
Anybody know what is the probelm of my code and any solutions that i could extract the information and save in a array and output on the screan line by line? many thanks for your help.
推荐答案
GNGGA, 152732 . 60 , 5257 . 1205318 ,N, 00111 . 0161931 ,W, 4 , 12 , 0 . 8 , 90 . 514 ,M ,, 0 . 91 , 0011 * 11
GNGGA,152732.60,5257.1205318,N,00111.0161931,W,4,12,0.8,90.514,M,,,0.91,0011*11
我想逐行读取数据并在字符串类中使用Split方法提取纬度(字符"N"之前的数字).我使用了以下代码,
I want to read the data line by line and use the Split method in string class to extract the latitude(the number before character ''N''). I used the following code,
string line;
line = sr.ReadLine();
while ((line = sr.ReadLine()) != null)
{
string[] temp = line.Split(',');
Console.WriteLine(temp[2]);
}
但是我在编译器中发现了以下错误.
but i found the following errors by the compiler.
Index was outside the bounds of the array.
我通过阅读数组教程来进行一天的研究,但我仍然对此感到困扰.
有人知道我的代码和可用来提取信息并将其保存在数组中并逐行输出到screan上的解决方案的解决方案吗?非常感谢您的帮助.
I work on it for one day by reading array tutorial,but i still bother with it.
Anybody know what is the probelm of my code and any solutions that i could extract the information and save in a array and output on the screan line by line? many thanks for your help.
这只是意味着数组元素temp[2]
不存在,因为temp
数组的元素少于3个.另外,不要忘记数组元素的索引是从0开始的.您永远不应假定从Split
或任何其他方式获得的数组中任何特定数量的元素.检查:if (temp.Length < expectedLength { blah_blah_blah(); /*...*/ }
.
—SA
This simply means that the array elementtemp[2]
does not exist becausetemp
array has less then 3 elements. Also, don''t forget the indexing of array element is 0-based. You should never assume any certain number of elements in array obtained fromSplit
or any other way. Check up:if (temp.Length < expectedLength { blah_blah_blah(); /*...*/ }
.
—SA
这篇关于GPS数据解析和数组异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!