0

I have to make a program where you ask user to enter any number of students, that asks the name and grade of each student. So if I said 2 students, I would put billy smith, then 54, then it would ask me the name of the 2nd student, john smith, then the grade, 81. Then it outputs the names with grades in descending order of grades. It would output:

name---------grades
------------------
John smith     81

billy smith   54

I have evrything except for it printing it out. I need it to print out the name with the grade. Here is what I have:

import java.util.*;

public class assignment5 {

public static void main(String[] args) {

//      Scanner for first name and last name with space in between.

java.util.Scanner input = new java.util.Scanner(System.in);

input.useDelimiter(System.getProperty("line.separator"));

        System.out.print("Enter the number of students: ");
        int numofstudents = input.nextInt();
        String[] names = new String[numofstudents];
        Double[] array = new Double[numofstudents];
        for(int i = 0; i < numofstudents; i++) {
            System.out.print("Enter the student's name: ");
            names[i] =input.next();
            System.out.print("Enter the student's score: ");
            array[i] = (Double) input.nextDouble();
        }
        System.out.print("Name" + "\tScore");
        System.out.print("\n----" + "\t----\n");
        selectionSort(names, array);
        System.out.println(Arrays.toString(names));

    }
    public static void selectionSort(String[] names, Double[] array) {
        for(int i = array.length - 1; i >= 1; i--) {
            String temp;
            Double currentMax = array[0];
            int currentMaxIndex = 0;
            for(int j = 1; j <= i; j++) {
                if (currentMax > array[j]) {
                    currentMax = array[j];
                    currentMaxIndex = j;
                }
            }       
            if (currentMaxIndex != i) {

                temp = names[currentMaxIndex];
                names[currentMaxIndex] = names[i];
                names[i] = temp;
                array[currentMaxIndex] = array[i];
                array[i] = currentMax;
            }
        }       
    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
lawtonhself
  • 41
  • 2
  • 2
  • 6
  • Create a Class to store the name/grade pairs, and then use a custom `Comparator` to sort the list, which would sort based on grade value. – Daniel Nugent Apr 27 '15 at 02:36

2 Answers2

0

This is very similar to value base sorted map. The below provided solution would be helpful.

Sort a Map<Key, Value> by values (Java)

Community
  • 1
  • 1
opera
  • 13
  • 3
0

This is one way to do it, create a Student class to store name/grade pairs, and use Arrays.sort() with a custom Comparator to sort in descending order after all the input is read:

import java.util.*;

public class assignment5 {

public static void main(String[] args) {

//      Scanner for first name and last name with space in between.

        java.util.Scanner input = new java.util.Scanner(System.in);

        input.useDelimiter(System.getProperty("line.separator"));

        System.out.print("Enter the number of students: ");
        int numofstudents = input.nextInt();
        //String[] names = new String[numofstudents]; //not needed
        //Double[] array = new Double[numofstudents]; //not needed
        Student[] students = new Student[numofstudents]; //added

        for(int i = 0; i < numofstudents; i++) {
            System.out.print("Enter the student's name: ");
            //names[i] =input.next();
            String student = input.next(); //added

            System.out.print("Enter the student's score: ");
            //array[i] = (Double) input.nextDouble();
            Double grade = (Double) input.nextDouble(); //added

            students[i] = new Student(student, grade); //added
        }
        System.out.print("Name" + "\tScore");
        System.out.print("\n----" + "\t----\n");
        //selectionSort(names, array);

        Arrays.sort(students, new Comparator<Student>() {

          @Override
          public int compare(Student entry1, Student entry2) {
            return entry2.grade.compareTo(entry1.grade); //sort by grade in descending order
          }
        });

        //System.out.println(Arrays.toString(students));
        for (int i = 0; i < students.length; i++){
          System.out.println(students[i].student + "\t" + students[i].grade);
        }

    }

}

public class Student{
  public String student;
  public Double grade;
  public Student(String s, Double g){
    this.student = s;
    this.grade = g;
  }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137