I need some help getting the Search method below to work with the menu, and I also cannot figure out how to get my bubble sort to work. I've spent a long time on this and think my brain is just fried at this point! :(

import java.util.*;

public class StudentProcessor2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // read in how many students to process System.out.print("How many students?"); int numStudents = input.nextInt(); Student[] students = new Student[numStudents]; for (int i = 0; i < numStudents; i++) { // read in student data from Scanner System.out.println("What is the students name?"); String name = input.next(); System.out.println("What is the students grade?"); // System.out.println("***Type -1 if no more grades***"); int grade = input.nextInt(); Student student = new Student(); student.setName(name); student.setGrade(grade); students[i] = student; } UserSelection(input, numStudents, students); } // show menu with options public static void UserSelection(Scanner input, int numStudents, Student[] students) { // Repeatedly process menu selections by the user. int choice; do { System.out.println(); System.out.println("*** Student Exam Results Menu ***"); System.out.println("(1) Display the results"); System.out.println("(2) Display the average result"); System.out.println("(3) Display the highest grade"); System.out.println("(4) Display the lowest grade"); System.out.println("(5) Search for specific result"); System.out.println("(6) Search for student grade by name"); System.out.println("(7) Sort the results in ascending order"); System.out.println("(8) quit"); choice = input.nextInt(); if (choice == 1) { System.out.println(Arrays.toString(students)); } else if (choice == 2) { double average = getAverage(students); System.out.println("average grade = " + average); } else if (choice == 3) { int high = getHighest(students); System.out.println("high grade = " + high); } else if (choice == 4) { int low = getLowest(students); System.out.println("low grade = " + low); } else if (choice == 5) { System.out.print("Result to look for: "); int grade = input.nextInt(); if (result(grade, students)) { System.out.println(grade + " is in the collection of grades."); } else { System.out.println(grade + " is not in the collection of grades."); } } else if (choice == 6) { System.out.print("Student to search for: "); String name = input.next(); if (search(name, students)) { System.out.println(name + " is in the list of Students."); } else { System.out.println(name + " is not in the list of Students"); } } } while (choice != 8); } // get Lower Grade private static int getLowest(Student[] students) { int lowest = 100; Student result = null; for (Student student : students) { if (student.getGrade() < lowest) { lowest = student.getGrade(); result = student; } } return result.getGrade(); } // get Highest grade private static int getHighest(Student[] students) { int highest = 0; Student result = null; for (Student student : students) { if (student.getGrade() > highest) { highest = student.getGrade(); result = student; } } return result.getGrade(); } // get Average grade private static double getAverage(Student[] students) { int total = 0; for (Student student : students) { total += student.getGrade(); } return total / students.length; } // Search for student private static Student search(String name, Student[] students) { Student result = null; for (Student student : students) { if (student.getName().equalsIgnoreCase(name)) { result = student; break; } } return result; } private static boolean result(int grade, Student[] students) { for (Student student : students) { if (student.getGrade() == grade) { return true; } } return false; } // !Bubble sort goes here! private void sort(Student[] students) { Student hold; // temporary holding area for swap for (int i = 0; i < students.length; i++) { // passes Student current = students[i]; Student next = students[i + 1]; if (current.getGrade() > next.getGrade()) // one comparison { hold = students[i]; // one swap students[i] = students[i + 1]; students[i + 1] = hold; } } } }

View Answers

Related Tutorials/Questions & Answers: