Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- FileStream fStream = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
- StreamReader inFile = new StreamReader(fStream);
- string inputRecord = "";
- string[] fields;
- int[] ageData = new int[1000];
- string[] genderData = new string[1000];
- string[] maritalData = new string[1000];
- int[] districtData = new int[1000];
- int[] countDist = new int[23];
- int[] ageGroup = new int[5];
- inputRecord = inFile.ReadLine();
- int i = 0;
- while (inputRecord != null)
- {
- fields = inputRecord.Split(',');
- ageData[i] = int.Parse(fields[0]);
- genderData[i] = fields[1];
- maritalData[i] = fields[2];
- districtData[i] = int.Parse(fields[3]);
- if (ageData[i] > 0 && ageData[i] <= 18)
- {
- ageGroup[0] = ageGroup[0] + 1;
- }
- if (ageData[i] > 18 && ageData[i] <= 30)
- {
- ageGroup[1] = ageGroup[1] + 1;
- }
- if (ageData[i] > 30 && ageData[i] <= 45)
- {
- ageGroup[2] = ageGroup[2] + 1;
- }
- if (ageData[i] > 45 && ageData[i] <= 64)
- {
- ageGroup[3] = ageGroup[3] + 1;
- }
- if (ageData[i] >= 65)
- {
- ageGroup[4] = ageGroup[4] + 1;
- }
- i++;
- inputRecord = inFile.ReadLine();
- }
- Console.WriteLine("This Program takes census data from a file and lists residents by age group and district");
- Console.WriteLine("Please ensure the file is formatted correctly! age,gender (M/F),marital status (M/S), and district");
- for (int x = 1; x <= 22; x++)
- for (int y = 0; y < districtData.Length; y++)
- if (districtData[y] == x)
- countDist[x]++;
- for (int x = 1; x <= 22; x++)
- Console.WriteLine("District " + x + " has " + countDist[x] + " citizens");
- Console.WriteLine("------Amount of Residents per Age Group--------");
- Console.WriteLine("Age Group 18 & under = {0}", ageGroup[0]);
- Console.WriteLine("Age Group 18-30 = {0}", ageGroup[1]);
- Console.WriteLine("Age Group 31-45 = {0}", ageGroup[2]);
- Console.WriteLine("Age Group 46-64 = {0}", ageGroup[3]);
- Console.WriteLine("Age Group 65 & over = {0}", ageGroup[4]);
- }
- if (ageData[i] > 0 && ageData[i] <= 18)
- {
- ageGroup[0] = ageGroup[0] + 1;
- }
- if (ageData[i] > 18 && ageData[i] <= 30)
- {
- ageGroup[1] = ageGroup[1] + 1;
- }
- if (ageData[i] > 0 && ageData[i] <= 18)
- {
- ageGroup[0] = ageGroup[0] + 1;
- }
- else if (ageData[i] <= 30) //I removed the "ageData[i] > 18 && "
- {
- ageGroup[1] = ageGroup[1] + 1;
- }
- string[] contents = System.IO.File.ReadAllLines(@"c:pathtofile.txt");
- foreach(string line in contents)
- {
- fields = line.Split(',');
- }
- for(int lineNumber = 0; lineNumber < contents.Length; lineNumber++
- {
- fields = contents[lineNumber].Split(',');
- //etc.
- }
- int[] ageData = new int[1000];
- ageData[i] = int.Parse(fields[0]);
- List<int> ageData = new List<int>();
- ageData.Add(int.Parse(fields[0]));
- //you have to add an int to the list before you can use it
- const int COLUMN_AGE = 0;
- const int ARRAY_SIZE = 1000;
- //etc.
- int[] ageData = new int[ARRAY_SIZE];
- ageData[i] = int.Parse(fields[COLUMN_AGE]);
- //etc.
- Console.WriteLine("District " + x + " has " + countDist[x] + " citizens");
- Console.WriteLine("District {0} has {1} citizens", x, countDist[x]);
- for (int x = 1; x <= 22; x++)
- for (int y = 0; y < districtData.Length; y++)
- if (districtData[y] == x)
- countDist[x]++;
- for (int districtCount = 1; districtCount <= 22; districtCount++)
- {
- for (int districtNumber = 0; districtNumber < districtData.Length; districtNumber++)
- {
- if (districtData[districtNumber] == districtCount)
- {
- countDist[districtCount]++;
- }
- }
- }
- public class Record
- {
- public int Age { get; set; }
- public string Gender { get; set; }
- public string MaritalStatus { get; set; }
- public int District { get; set; }
- }
- var records = new List<Record>();
- var inputRecord = inFile.ReadLine();
- while (inputRecord != null)
- {
- var fields = inputRecord.Split(',');
- var currentRecord = new Record()
- {
- Age = int.Parse(fields[0]),
- Gender = fields[1],
- MaritalStatus = fields[2],
- District = int.Parse(fields[3])
- };
- records.Add(currentRecord);
- //...
- }
- using (var fStream = new FileStream("test.txt", FileMode.Open, FileAccess.Read))
- {
- using (var inFile = new StreamReader(fStream))
- {
- //Work with the file here
- //...
- } //inFile.Dispose() happens here automatically.
- } //fStream.Dispose() happens here automatically.
- for (int x = 1; x <= 22; x++)
- for (int y = 0; y < districtData.Length; y++)
- if (districtData[y] == x)
- countDist[x]++;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement