Results 1 to 5 of 5
 03202013, 08:10 PM #1Member
 Join Date
 Mar 2013
 Posts
 2
 Rep Power
 0
Please help me to initialise my bubble sort
So my code is pretty solid now (I think), and I know I'm missing something fairly simple here but I cannot figure out how to get the option 7 of my menu to use my sort method to sort the students array and then printed the sorted grades to the screen. Can I get some help please?
Java Code: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) !=null) { System.out.println(name + " is in the list of Students."); } else { System.out.println(name + " is not in the list of Students"); } }else if (choice == 7) { int order = sort(students); System.out.println("The results in ascending order are: " + sort); } } 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; } // Search for grade private static boolean result(int grade, Student[] students) { for (Student student : students) { if (student.getGrade() == grade) { return true; } } return false; } // !Bubble sort goes here! public static void sort(Student[] students) { int j; boolean flag = true; // set flag to true to begin first pass Student temp; //holding variable while (flag) { flag = false; //set flag to false awaiting a possible swap for (j = 0; j < students.length 1; j++) { if (students[j].compareTo(students[j+1]) > 0) // change to > for ascending sort { temp = students[j]; //swap elements students[j] = students[j+1]; students[j+1] = temp; flag = true; //shows a swap occurred } } } } }
 03202013, 09:04 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
Re: Please help me to initialise my bubble sort
Your code assumes that your sort( ... ) method returns an int value; it doesn't, it returns a void (nothing).
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03202013, 09:11 PM #3Senior Member
 Join Date
 Oct 2012
 Posts
 108
 Rep Power
 0
Re: Please help me to initialise my bubble sort
Java Code:int order = sort(students); System.out.println("The results in ascending order are: " + sort);
What is "sort" in the println statement? You'll want a function (or some other mechanism hint: for(Student s: students) ) to print the Array; one element at a time.
 03212013, 12:02 AM #4Member
 Join Date
 Mar 2013
 Posts
 2
 Rep Power
 0
Re: Please help me to initialise my bubble sort
I've added this and it is compiling now:
Java Code:}else if (choice == 7) { for (Student student : students) { System.out.println("The results in ascending order are: " + students); } }
The results in ascending order are: [LStudent;@1b67f74
The results in ascending order are: [LStudent;@1b67f74
 03212013, 12:09 AM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,651
 Rep Power
 9
Re: Please help me to initialise my bubble sort
Change students to student in your print statement.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Bubble sort Algorithm
By Hotspotmar in forum New To JavaReplies: 1Last Post: 06062012, 07:38 AM 
Bubblesort method
By Mapisto in forum New To JavaReplies: 10Last Post: 12272011, 10:43 PM 
Bubble sort
By pineapple in forum New To JavaReplies: 3Last Post: 04252009, 12:45 AM 
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04292008, 08:04 PM 
need help with bubble sort
By lowpro in forum New To JavaReplies: 3Last Post: 12172007, 06:27 PM
Bookmarks