Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. FileStream fStream = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
  4. StreamReader inFile = new StreamReader(fStream);
  5.  
  6. string inputRecord = "";
  7. string[] fields;
  8. int[] ageData = new int[1000];
  9. string[] genderData = new string[1000];
  10. string[] maritalData = new string[1000];
  11. int[] districtData = new int[1000];
  12. int[] countDist = new int[23];
  13. int[] ageGroup = new int[5];
  14.  
  15. inputRecord = inFile.ReadLine();
  16.  
  17. int i = 0;
  18. while (inputRecord != null)
  19. {
  20. fields = inputRecord.Split(',');
  21.  
  22. ageData[i] = int.Parse(fields[0]);
  23. genderData[i] = fields[1];
  24. maritalData[i] = fields[2];
  25. districtData[i] = int.Parse(fields[3]);
  26.  
  27. if (ageData[i] > 0 && ageData[i] <= 18)
  28. {
  29. ageGroup[0] = ageGroup[0] + 1;
  30. }
  31. if (ageData[i] > 18 && ageData[i] <= 30)
  32. {
  33. ageGroup[1] = ageGroup[1] + 1;
  34. }
  35. if (ageData[i] > 30 && ageData[i] <= 45)
  36. {
  37. ageGroup[2] = ageGroup[2] + 1;
  38. }
  39. if (ageData[i] > 45 && ageData[i] <= 64)
  40. {
  41. ageGroup[3] = ageGroup[3] + 1;
  42. }
  43. if (ageData[i] >= 65)
  44. {
  45. ageGroup[4] = ageGroup[4] + 1;
  46. }
  47.  
  48. i++;
  49.  
  50. inputRecord = inFile.ReadLine();
  51. }
  52. Console.WriteLine("This Program takes census data from a file and lists residents by age group and district");
  53. Console.WriteLine("Please ensure the file is formatted correctly! age,gender (M/F),marital status (M/S), and district");
  54. for (int x = 1; x <= 22; x++)
  55. for (int y = 0; y < districtData.Length; y++)
  56. if (districtData[y] == x)
  57. countDist[x]++;
  58.  
  59. for (int x = 1; x <= 22; x++)
  60. Console.WriteLine("District " + x + " has " + countDist[x] + " citizens");
  61.  
  62.  
  63.  
  64. Console.WriteLine("------Amount of Residents per Age Group--------");
  65. Console.WriteLine("Age Group 18 & under = {0}", ageGroup[0]);
  66. Console.WriteLine("Age Group 18-30 = {0}", ageGroup[1]);
  67. Console.WriteLine("Age Group 31-45 = {0}", ageGroup[2]);
  68. Console.WriteLine("Age Group 46-64 = {0}", ageGroup[3]);
  69. Console.WriteLine("Age Group 65 & over = {0}", ageGroup[4]);
  70. }
  71.  
  72. if (ageData[i] > 0 && ageData[i] <= 18)
  73. {
  74. ageGroup[0] = ageGroup[0] + 1;
  75. }
  76. if (ageData[i] > 18 && ageData[i] <= 30)
  77. {
  78. ageGroup[1] = ageGroup[1] + 1;
  79. }
  80.  
  81. if (ageData[i] > 0 && ageData[i] <= 18)
  82. {
  83. ageGroup[0] = ageGroup[0] + 1;
  84. }
  85. else if (ageData[i] <= 30) //I removed the "ageData[i] > 18 && "
  86. {
  87. ageGroup[1] = ageGroup[1] + 1;
  88. }
  89.  
  90. string[] contents = System.IO.File.ReadAllLines(@"c:pathtofile.txt");
  91.  
  92. foreach(string line in contents)
  93. {
  94. fields = line.Split(',');
  95. }
  96.  
  97. for(int lineNumber = 0; lineNumber < contents.Length; lineNumber++
  98. {
  99. fields = contents[lineNumber].Split(',');
  100. //etc.
  101. }
  102.  
  103. int[] ageData = new int[1000];
  104. ageData[i] = int.Parse(fields[0]);
  105.  
  106. List<int> ageData = new List<int>();
  107. ageData.Add(int.Parse(fields[0]));
  108. //you have to add an int to the list before you can use it
  109.  
  110. const int COLUMN_AGE = 0;
  111. const int ARRAY_SIZE = 1000;
  112. //etc.
  113.  
  114. int[] ageData = new int[ARRAY_SIZE];
  115. ageData[i] = int.Parse(fields[COLUMN_AGE]);
  116. //etc.
  117.  
  118. Console.WriteLine("District " + x + " has " + countDist[x] + " citizens");
  119.  
  120. Console.WriteLine("District {0} has {1} citizens", x, countDist[x]);
  121.  
  122. for (int x = 1; x <= 22; x++)
  123. for (int y = 0; y < districtData.Length; y++)
  124. if (districtData[y] == x)
  125. countDist[x]++;
  126.  
  127. for (int districtCount = 1; districtCount <= 22; districtCount++)
  128. {
  129. for (int districtNumber = 0; districtNumber < districtData.Length; districtNumber++)
  130. {
  131. if (districtData[districtNumber] == districtCount)
  132. {
  133. countDist[districtCount]++;
  134. }
  135. }
  136. }
  137.  
  138. public class Record
  139. {
  140. public int Age { get; set; }
  141. public string Gender { get; set; }
  142. public string MaritalStatus { get; set; }
  143. public int District { get; set; }
  144. }
  145.  
  146. var records = new List<Record>();
  147. var inputRecord = inFile.ReadLine();
  148.  
  149. while (inputRecord != null)
  150. {
  151. var fields = inputRecord.Split(',');
  152. var currentRecord = new Record()
  153. {
  154. Age = int.Parse(fields[0]),
  155. Gender = fields[1],
  156. MaritalStatus = fields[2],
  157. District = int.Parse(fields[3])
  158. };
  159.  
  160. records.Add(currentRecord);
  161. //...
  162. }
  163.  
  164. using (var fStream = new FileStream("test.txt", FileMode.Open, FileAccess.Read))
  165. {
  166. using (var inFile = new StreamReader(fStream))
  167. {
  168. //Work with the file here
  169. //...
  170. } //inFile.Dispose() happens here automatically.
  171. } //fStream.Dispose() happens here automatically.
  172.  
  173. for (int x = 1; x <= 22; x++)
  174. for (int y = 0; y < districtData.Length; y++)
  175. if (districtData[y] == x)
  176. countDist[x]++;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement