Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear guys,

I was recently programming for GPS data parsing.

GPS data format is as follows:
C++
$GNGGA,152732.60,5257.1205318,N,00111.0161931,W,4,12,0.8,90.514,M,,,0.91,0011*11


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,
C#
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.

SQL
Index was outside the bounds of the array.


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.
Posted

1 solution

This simply means that the array element temp[2] does not exist because temp 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 from Split or any other way. Check up: if (temp.Length < expectedLength { blah_blah_blah(); /*...*/ }.

—SA
 
Share this answer
 
v2
Comments
HAOYE850815 28-Feb-11 6:12am    
Hello SA,

Thanks for your reply. Actually what I want is to split the string of $GNGGA data into different substrings, and then extract the usful information(some substrings in this string).
But i found that, because the split method split strings into many substrings and stored line by line, with 0-based index. If I tried to display it in Console Program, it is all right like StreamReader sr = new StreamReader("data1.txt");
string data = sr.ReadToEnd();
string[] lineArr = data.Split('$');
for (int i = 0; i < lineArr.Length; i++)
{
string strTemp = lineArr[i];
string[] words = strTemp.Split(',');
if (words[0] == "GPGGA")
{
try
{
Console.WriteLine(words[1]);
Console.WriteLine(words[2]);
Console.WriteLine(words[4]);
}
catch
{
}
}
}
but the situation is changed if i want to output into WindowsForm TextBox, I used anther function to packed this data processing
private static double GetLat (string sentence)
{
string[] words = sentence.Split(',');
foreach (string s in words)
{
double latitude = Convert.ToDouble(s[2]);
}
return latitude;
}
to split the strings and output the THIRD value, it doesn't work and always break like "Index was outside the bounds of the array". I understand this possibly a simple problem, but it really disturbed me for a few days. can you provide any more suggestions or usful code explanations? Many thanks.
Sergey Alexandrovich Kryukov 1-Mar-11 3:15am    
Run under debugger and you will see what's going on. You do the same mistake all the time: assume more elements in array then actually are.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900