Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by rwhalsey on 10/3/2016.
- */
- import java.io.File;
- import java.io.IOException;
- import java.nio.file.Paths;
- import java.util.Scanner;
- public class Grades {
- public static void main(String [] args) throws IOException {
- File file = Paths.get(".", "resources", "Names.txt").normalize().toFile();
- File fileTwo = Paths.get(".", "resources", "Grades.txt").normalize().toFile();
- // File file = new File(fileName); // create a File object
- if ( file.exists() ) // check that the file exists
- { // before trying to create a
- // Scanner to read the file
- // Create a Scanner from the file.
- // This statement can cause a FileNotFoundException.
- Scanner inFile = new Scanner( file );
- // For each line in the file, read in the line and display it with the line number
- // int lineNum = 0;
- // Use the results of calling the hasNext method to
- // determine if you are at the end of the file before
- // reading the next line of the file.
- String name;
- //get the length of the names array
- int name_count = 0;
- while ( inFile.hasNext() )
- {
- name = inFile.nextLine(); // read the next line
- name_count ++;
- }
- // When we're done reading the file,
- // close the Scanner object attached to the file
- inFile.close();
- //create new scanner to read the file again
- Scanner inFileTwo = new Scanner( file );
- //create the names array
- String[] names = new String[name_count];
- //fill the names array
- for (int k = 0; k < names.length; k ++){
- name = inFileTwo.nextLine();
- names[k] = name;
- }
- // When we're done reading the file,
- // close the Scanner object attached to the file
- inFileTwo.close();
- //Now read the grades file
- Scanner inFileThree = new Scanner( fileTwo );
- //initialize an accumulator
- int grades_count = 0;
- while ( inFileThree.hasNext() )
- {
- String grade = inFileThree.nextLine(); // read the next line
- grades_count ++; //accumulate
- }
- // When we're done reading the file,
- // close the Scanner object attached to the file
- inFileThree.close();
- if (grades_count != name_count)
- {
- System.out.println("There are unequal grades to names. Please check the text files and try again.");
- }
- else
- {
- //Now read the grades file again
- Scanner inFileFour = new Scanner(fileTwo);
- //create the grades array
- int[] grades = new int[grades_count];
- //fill the grades array
- for (int k = 0; k < names.length; k++) {
- String grade = inFileFour.nextLine();
- int gradeInt = Integer.valueOf(grade);
- grades[k] = gradeInt;
- }
- // When we're done reading the file,
- // close the Scanner object attached to the file
- inFileFour.close();
- //step 1 is done now. names[k] is names, and grades[k] is grades.
- //also step 2 was done at the same time, and the values were saved to the arrays.
- //now its time for step 3. load the methods.
- display_a_grade_count(grades);
- display_b_grade_count(grades);
- display_who_has_highest_grade(grades,names);
- display_who_has_lowest_grade(grades,names);
- display_the_average_grade(grades);
- //step four
- prompt_user_for_individuals_grade(grades,names);
- }
- }
- }
- public static void display_a_grade_count(int[] grades){
- int a_count = 0;
- int lowest_grade = 90;
- int highest_grade = 100;
- for (int x = 0; x < grades.length; x ++){
- int cur_grade = grades[x];
- if (cur_grade >= lowest_grade && cur_grade <= highest_grade){
- a_count ++;
- }
- }
- System.out.println("There are " + a_count + " people who earned an A.");
- }
- public static void display_b_grade_count(int[] grades){
- int b_count = 0;
- int lowest_grade = 80;
- int highest_grade = 89;
- for (int x = 0; x < grades.length; x ++){
- int cur_grade = grades[x];
- if (cur_grade >= lowest_grade && cur_grade <= highest_grade){
- b_count ++;
- }
- }
- System.out.println("There are " + b_count + " people who earned a B.");
- }
- public static void display_who_has_highest_grade(int[] grades, String[] names){
- String highest_name = "";
- double highest_grade = Double.NEGATIVE_INFINITY;
- for (int x = 0; x < grades.length; x ++){
- double cur_grade = (double)grades[x];
- if (cur_grade > highest_grade){
- highest_grade = cur_grade;
- highest_name = names[x];
- }
- }
- int highest = (int) highest_grade;
- System.out.println("The highest grade was a " + highest + " which was given to " + highest_name + ".");
- }
- public static void display_who_has_lowest_grade(int[] grades, String[] names){
- String lowest_name = "";
- double lowest_grade = Double.POSITIVE_INFINITY;
- for (int x = 0; x < grades.length; x ++){
- double cur_grade = (double)grades[x];
- if (cur_grade < lowest_grade){
- lowest_grade = cur_grade;
- lowest_name = names[x];
- }
- }
- int lowest = (int) lowest_grade;
- System.out.println("The lowest grade was a " + lowest + " which was given to " + lowest_name + ".");
- }
- public static void display_the_average_grade (int[] grades){
- int accumulator = 0;
- int total = 0;
- for (int x = 1; x < grades.length; x ++){
- total += grades[x];
- accumulator ++;
- }
- int average = total / accumulator;
- System.out.println("The average grade is " + average + ".");
- }
- public static void prompt_user_for_individuals_grade (int[] grades, String[] names){
- Scanner input = new Scanner(System.in);
- boolean cont = true;
- while (cont = true) {
- System.out.println("");
- System.out.print("Which individual's grade would you like to check?");
- String cur_name = input.nextLine();
- int test = 0;
- int index = 0;
- for (int x = 0; x < names.length; x ++){
- if (names[x].equals(cur_name)){
- test = 1;
- index = x;
- }
- }
- if (test == 0){
- System.out.println("That name isn't in the database. Please try again.");
- }
- else if (test == 1){
- System.out.println(names[index] + " had a grade of " + grades[index]);
- cont = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement