Question:
Java Array Sorting + Constructor?
?
2009-12-07 19:52:08 UTC
I have to sort an array of students, based on their midterm score values, that was created by tokenizing a given input.txt file.

The very last line (noted) is giving me a syntax error and I'm pretty sure it's just that my logic is off but I can't figure it out. The error says "Type mismatch: cannot convert from double to Student".

This is my sorting method so far:

public static Student[] sortStudents(Student[] std){

//You need to write code for sorting the student array
for (int i = std.length - 1; i >= 1; i--)
{
double currentMax = std[0].getMidScore();
int currentMaxIndex = 0;

for (int j = 1; j <= i; j++)
{
if (currentMax < std[j].getMidScore())
{
currentMax = std[j].getMidScore();
currentMaxIndex = j;
}
}

if (currentMaxIndex != i)
{
std[currentMaxIndex] = std[i];
std[i] = currentMax; // This line is giving me the error
}
}
Three answers:
SPB
2009-12-07 20:02:09 UTC
You have std defined as an array of Student objects and you are trying to put a double into it. Can't do that.
Derp
2009-12-07 20:04:01 UTC
The line

std[i] = currentMax;

is trying to assign the student object at array location i (say student #5) to currentMax, a double.



You're trying to stick a student in a double.



The line should be currentMax = std[i].getMidScore();
barbier
2016-11-15 15:48:07 UTC
thank you for an in intensity question! without offering code it rather is very complicated to savor the situation the priority is. are you able to located up the aspects of your code the situation .. the arrays are declared.. they're initialised .. and the situation the values are set.. even if if, in case you digital mail me the comprehensive sort i'd desire to have a radical seem. in fact click on my username and grant me an digital mail


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...