|
-
May 12th, 2005, 07:52 AM
#1
Thread Starter
New Member
Help with my code - Distance Finder Program
This is what i got so far:
Code:
//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
Last edited by Hack; May 13th, 2005 at 12:48 PM.
-
May 12th, 2005, 08:10 PM
#2
Fanatic Member
Re: Help with my code - Distance Finder Program
First off, you can format your code with [code][/code]. You can cast it by (char)IntegralValue not (Char)IntegralValue. If you want to loop through the splitted values. Just do a
VB Code:
foreach(string temp in [i]ArrayOfString[/i]) . . .
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. . .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|