Advertisement
Ramaraunt1

Untitled

Oct 3rd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. /**
  2. * Created by rwhalsey on 10/3/2016.
  3. */
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.nio.file.Paths;
  7. import java.util.Scanner;
  8.  
  9. public class Grades {
  10. public static void main(String [] args) throws IOException {
  11.  
  12.  
  13.  
  14.  
  15.  
  16. File file = Paths.get(".", "resources", "Names.txt").normalize().toFile();
  17. File fileTwo = Paths.get(".", "resources", "Grades.txt").normalize().toFile();
  18.  
  19. // File file = new File(fileName); // create a File object
  20.  
  21.  
  22.  
  23. if ( file.exists() ) // check that the file exists
  24. { // before trying to create a
  25. // Scanner to read the file
  26. // Create a Scanner from the file.
  27. // This statement can cause a FileNotFoundException.
  28. Scanner inFile = new Scanner( file );
  29.  
  30. // For each line in the file, read in the line and display it with the line number
  31. // int lineNum = 0;
  32.  
  33. // Use the results of calling the hasNext method to
  34. // determine if you are at the end of the file before
  35. // reading the next line of the file.
  36. String name;
  37.  
  38. //get the length of the names array
  39. int name_count = 0;
  40. while ( inFile.hasNext() )
  41. {
  42. name = inFile.nextLine(); // read the next line
  43.  
  44. name_count ++;
  45. }
  46.  
  47. // When we're done reading the file,
  48. // close the Scanner object attached to the file
  49. inFile.close();
  50.  
  51. //create new scanner to read the file again
  52. Scanner inFileTwo = new Scanner( file );
  53.  
  54. //create the names array
  55. String[] names = new String[name_count];
  56.  
  57. //fill the names array
  58. for (int k = 0; k < names.length; k ++){
  59. name = inFileTwo.nextLine();
  60.  
  61. names[k] = name;
  62. }
  63.  
  64. // When we're done reading the file,
  65. // close the Scanner object attached to the file
  66. inFileTwo.close();
  67.  
  68. //Now read the grades file
  69. Scanner inFileThree = new Scanner( fileTwo );
  70.  
  71. //initialize an accumulator
  72. int grades_count = 0;
  73.  
  74. while ( inFileThree.hasNext() )
  75. {
  76. String grade = inFileThree.nextLine(); // read the next line
  77.  
  78. grades_count ++; //accumulate
  79. }
  80.  
  81. // When we're done reading the file,
  82. // close the Scanner object attached to the file
  83. inFileThree.close();
  84.  
  85. if (grades_count != name_count)
  86. {
  87. System.out.println("There are unequal grades to names. Please check the text files and try again.");
  88. }
  89. else
  90. {
  91.  
  92.  
  93. //Now read the grades file again
  94. Scanner inFileFour = new Scanner(fileTwo);
  95.  
  96. //create the grades array
  97. int[] grades = new int[grades_count];
  98.  
  99. //fill the grades array
  100. for (int k = 0; k < names.length; k++) {
  101. String grade = inFileFour.nextLine();
  102.  
  103. int gradeInt = Integer.valueOf(grade);
  104. grades[k] = gradeInt;
  105. }
  106.  
  107. // When we're done reading the file,
  108. // close the Scanner object attached to the file
  109. inFileFour.close();
  110.  
  111. //step 1 is done now. names[k] is names, and grades[k] is grades.
  112. //also step 2 was done at the same time, and the values were saved to the arrays.
  113. //now its time for step 3. load the methods.
  114.  
  115. display_a_grade_count(grades);
  116. display_b_grade_count(grades);
  117. display_who_has_highest_grade(grades,names);
  118. display_who_has_lowest_grade(grades,names);
  119. display_the_average_grade(grades);
  120.  
  121. //step four
  122. prompt_user_for_individuals_grade(grades,names);
  123.  
  124. }
  125. }
  126.  
  127.  
  128. }
  129.  
  130. public static void display_a_grade_count(int[] grades){
  131.  
  132. int a_count = 0;
  133. int lowest_grade = 90;
  134. int highest_grade = 100;
  135.  
  136. for (int x = 0; x < grades.length; x ++){
  137.  
  138. int cur_grade = grades[x];
  139. if (cur_grade >= lowest_grade && cur_grade <= highest_grade){
  140. a_count ++;
  141. }
  142. }
  143.  
  144. System.out.println("There are " + a_count + " people who earned an A.");
  145.  
  146.  
  147. }
  148.  
  149. public static void display_b_grade_count(int[] grades){
  150.  
  151. int b_count = 0;
  152. int lowest_grade = 80;
  153. int highest_grade = 89;
  154.  
  155. for (int x = 0; x < grades.length; x ++){
  156.  
  157. int cur_grade = grades[x];
  158. if (cur_grade >= lowest_grade && cur_grade <= highest_grade){
  159. b_count ++;
  160. }
  161. }
  162.  
  163. System.out.println("There are " + b_count + " people who earned a B.");
  164.  
  165.  
  166. }
  167.  
  168. public static void display_who_has_highest_grade(int[] grades, String[] names){
  169.  
  170. String highest_name = "";
  171. double highest_grade = Double.NEGATIVE_INFINITY;
  172.  
  173. for (int x = 0; x < grades.length; x ++){
  174. double cur_grade = (double)grades[x];
  175.  
  176. if (cur_grade > highest_grade){
  177. highest_grade = cur_grade;
  178. highest_name = names[x];
  179. }
  180.  
  181. }
  182.  
  183. int highest = (int) highest_grade;
  184.  
  185. System.out.println("The highest grade was a " + highest + " which was given to " + highest_name + ".");
  186. }
  187.  
  188. public static void display_who_has_lowest_grade(int[] grades, String[] names){
  189.  
  190. String lowest_name = "";
  191. double lowest_grade = Double.POSITIVE_INFINITY;
  192.  
  193. for (int x = 0; x < grades.length; x ++){
  194. double cur_grade = (double)grades[x];
  195.  
  196. if (cur_grade < lowest_grade){
  197. lowest_grade = cur_grade;
  198. lowest_name = names[x];
  199. }
  200.  
  201. }
  202.  
  203. int lowest = (int) lowest_grade;
  204.  
  205. System.out.println("The lowest grade was a " + lowest + " which was given to " + lowest_name + ".");
  206. }
  207.  
  208. public static void display_the_average_grade (int[] grades){
  209.  
  210. int accumulator = 0;
  211. int total = 0;
  212. for (int x = 1; x < grades.length; x ++){
  213. total += grades[x];
  214. accumulator ++;
  215. }
  216. int average = total / accumulator;
  217.  
  218. System.out.println("The average grade is " + average + ".");
  219. }
  220.  
  221. public static void prompt_user_for_individuals_grade (int[] grades, String[] names){
  222.  
  223. Scanner input = new Scanner(System.in);
  224.  
  225. boolean cont = true;
  226.  
  227. while (cont = true) {
  228.  
  229. System.out.println("");
  230. System.out.print("Which individual's grade would you like to check?");
  231. String cur_name = input.nextLine();
  232.  
  233. int test = 0;
  234. int index = 0;
  235.  
  236. for (int x = 0; x < names.length; x ++){
  237.  
  238. if (names[x].equals(cur_name)){
  239. test = 1;
  240. index = x;
  241. }
  242. }
  243.  
  244. if (test == 0){
  245. System.out.println("That name isn't in the database. Please try again.");
  246. }
  247. else if (test == 1){
  248. System.out.println(names[index] + " had a grade of " + grades[index]);
  249. cont = false;
  250. }
  251. }
  252.  
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement