Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java Sorting Students in Decreasing Order by Score (http://www.go4expert.com/forums/java-sorting-students-decreasing-score-t25548/)

MoonBeam080680 17Apr2011 10:13

Java Sorting Students in Decreasing Order by Score
I am trying to get this program to sort the students in decreasing order by their score. I keep getting an error message when I try to use SelectionSort. This is the error I receive.
C:\Users\Jen>javac Sort.java
Sort.java:36: selectionSort(int[]) in Sort cannot be applied to (int)
int sortOutput = selectionSort(scores[i]);
1 error

Here is the code I am using. Any help would be greatly appreciated.


import java.util.*;
public class Sort {
 public static void main(String[] args){
  //Create Scanner
  Scanner input = new Scanner(System.in);
  //Get the number of students
  System.out.print("Please enter the number of students: ");
  int numberOfStudents;
  String temp = input.nextLine();
  numberOfStudents = Integer.parseInt(temp);
  //Create Array
  int[] scores = new int[numberOfStudents];
  String [] names = new String[numberOfStudents];
  Scanner input2 = new Scanner(System.in);
  //Loops asking user to enter data
  for (int i = 0; i < names.length; i ++){
    System.out.print("Please enter a name: ");
    names[i] = input.nextLine();
    System.out.print("Please enter a score for the name: ");
    scores[i] = input2.nextInt();
  System.out.println("Students Highest to Lowest");
  for (int i = 0; i < names.length; i++){
  String output = " ";
  output = names[i] + " score is " + scores[i] + "\n";
  int sortOutput = selectionSort(scores[i]);
 public static void selectionSort(int [] scores){
  for (int i = scores.length - 1; i >= 1; i--){
  //Find the max in the scores array
  int max = scores[0];
  int maxIndex = 0;
  for (int j = 1; j <= i; j++){
    if(max < scores[j]){
    max = scores[j];
    maxIndex = j;
    // Swap scores as necessary
  if (maxIndex != i){
    scores[maxIndex] = scores[i];
    scores[i] = max;

ManzZup 21Apr2011 21:54

Re: Java Sorting Students in Decreasing Order by Score
why are you making the selection sort?
the arrays of java contains the sort method

szilla211 6Jul2012 00:16

Re: Java Sorting Students in Decreasing Order by Score
the main thing i would say is you initialized 2 arrays in main but you are only passing one to selection sort. you need to pass names[] and scores[] to selection sort for it to work properly. also ur error comes from the fact that u are trying to stuff an array into a primitive type variable. i would delete that entirely.

lemme know if this helps :)

All times are GMT +5.5. The time now is 16:05.