PDA

Click to See Complete Forum and Search --> : Help with my code - Distance Finder Program


rob316
May 12th, 2005, 07:52 AM
This is what i got so far:
//Open the files
System.IO.StreamReader fileCities = new System.IO.StreamReader
(@"C:\Cities.txt");
System.IO.StreamReader fileDistances = new System.IO.StreamReader
(@"C:\Distances.txt");


//Read the files
string strCities = fileCities.ReadToEnd();
string strDistances = fileDistances.ReadToEnd();

//Close the files
fileCities.Close();
fileDistances.Close();

//Split Cities into an array of lines using ASCII 10 as the delimiter

string[] strCitiesLines = strCities.Split((Char)10);
string[] strDistancesLines = strDistances.Split((Char)10);

//Set up the array for the distances matrix

lngDistances = new long[strCitiesLines.Length - 1, strCitiesLines.Length - 1];

//Collect city name without whitespace

strCities = strCitiesLines[intCities].Trim();

//Check it has a valid length

if(strCities.Length > 0)
{
//Add to both of the text boxes

cmbStart.Items.Add(strCities);
cmbFinish.Items.Add(strCities);

//Collect the distance values for each city

strDistances = strDistancesLines[intCities].Split(Convert.ToChar(","));

//Add the values to the distances array

lngDistances[intCities - 1, intDistance] = long.Parse(strDistances[intDistance]);

//Get the result from the array

try
{
txtDistance.Text = lngDistances[cmbStart.SelectedIndex,cmbStart.SelectedIndex.ToString()];
}
catch(Exception ex)
{
MessageBox.Show("Error - Please select the 2 cities first");
}


I'm getting errors, i need to know how to correct these. I know i need to add a loop but not sure how. Help anyone?

Edit: Added formatting for clarity - Hack

nebulom
May 12th, 2005, 08:10 PM
First off, you can format your code with . You can cast it by (char)IntegralValue not (Char)IntegralValue. If you want to loop through the splitted values. Just do a

foreach(string temp in ArrayOfString) . . .

From there you can do the rest. Hope it helps.

Edit: You can also split it by character/escape sequence. Split('\n') or Split('\t'), etc. . .