Ive been attempting to complete a project which is due in a number of hours (yeah im an idiot leaving it this late) where I have to take information on properties.
Ive been trying to make a graph on how many times each county is entered. The information is stored on a text file with "," delimiters. Every 8th value should be the name of a county. The code ive been using is stated below:
using (StreamReader sr = new StreamReader("base.txt"))
{
while ((line = sr.ReadLine()) != null)
{
string[] delimiters = new string[] { "," };
while(delimiters[i] != null)
{
if(delimiters[i] == "Antrim")
{
antrimPoints++;
}
else if(delimiters[i] == "Armagh")
{
armaghPoints++;
}
Man thats a clutzy mistake. For some reason I thought that piece of code would do it all for me. Thanks for the advise.
Im trying to read the text file into an array automatically. Is there any easy way to do that? I don't see any information on that in your links.
And on a side note. Im using Visual Studio 2008. Is there any way to remove the "x" button from the top right of my forms? Id like the users to have to use an in-form button to exit the application.
For the X button change the ControlBox property on the form to False. Can you post an example of the file you're trying to read in and I'll take a look at converting it into an array for you.
//Indicate what position in each line the county appears in
const int COUNTY_INDEX = 1;
//Prepare our delimiter
char[] delimiter = new char[] { ',' };
//Prepare our dictionary for storing a count for each county
Dictionary<string, int> countyCount = new Dictionary<string, int>();
using (StreamReader sr = new StreamReader("base.txt"))
{
do
{
//Extract the next line from the file
//and pull out the county
string line = sr.ReadLine();
string[] parts = line.Split(delimiter);
string county = parts[COUNTY_INDEX];
//Make a note of the count for this county
if (countyCount.ContainsKey(county))
{
countyCount[county]++;
}
else
{
countyCount.Add(county, 1);
}
} while (!sr.EndOfStream);
}
foreach (KeyValuePair<string, int> county in countyCount)
{
string name = county.Key;
int number = county.Value;
//***** Now do what you like with name and number
}
Ive entered the code and my form no longer errors. Im not sure how exactly to access each counties count however. Sorry for sounding a bit silly.
If say twelve antrim properties had been entered, would "antrim.Value" display the number 12? Once I have access to the number I can definitely make the bar charts easily enough.
All the data you need is in the countyCount dictionary.
Put a break point in the final foreach loop I wrote and you'll see that the name variable will step through the names our your counties and the number will be the count for that county.
If you want to access Antrim directly then use
countyCount["Antrim"]
This will return an int with the count.
I wouldn't recommend going down that route of hard coding 'Antrim' in your code because your code won't be very flexible. Better off looping through the dictionary and building your graph from that.
Bookmarks