i've iterated through them like 4 times and it still looks right to me. Could it be the brackets being in wrong places or something. im trying to sort my array of scholars in this method, printAllApplicantsByName() and then print them. The first 2 for loops dont matter, it just takes the indexes that arent null and copies them to a temporary array and copies them back so they are ready to sort.
lets say the last names are in this order(i'll only give you first letter, the other letters dont match in any way anyways):
scholar[0] = "c"; scholar[1] = "b"; scholar[2] = "w"; scholar[3] = "a";
scholar[4] = "j"; scholar[5] = "m";
also i'll give you my compareNames() method, since i already have a compareTo() for something else, but i beleive its correct, my professor went through that with me.
i put where to start iterating through in a comment, i believe its the 3rd for loop in printAllApplicantsByName()
Thanks in advance.
lets say the last names are in this order(i'll only give you first letter, the other letters dont match in any way anyways):
scholar[0] = "c"; scholar[1] = "b"; scholar[2] = "w"; scholar[3] = "a";
scholar[4] = "j"; scholar[5] = "m";
also i'll give you my compareNames() method, since i already have a compareTo() for something else, but i beleive its correct, my professor went through that with me.
Code:
public int compareNames(Object other, Object myObject){
int result = 0;
String myName = ((Scholar)myObject).reverseFullName();
String otherName = ((Scholar) other).reverseFullName();
String myFirstName = "";
String otherFirstName = "";
String myLastName = "";
String otherLastName = "";
myName = myName.toLowerCase();
otherName = otherName.toLowerCase();
for(int i = 0; i < myName.length(); i++){
if(myName.charAt(i) != ',')
myLastName += myName.charAt(i);
if(myName.charAt(i) == ',')
break;
System.out.print(myLastName);
}
int counter = 0;
for(int j = 0; j < otherName.length(); j++){
if(otherName.charAt(j) == ',')
break;
otherLastName += otherName.charAt(j);
System.out.println(otherLastName);
counter++;
}
System.out.println('\n');
for(int k = 0; k < myName.length(); k++){
if(myName.charAt(k) != ' ')
myFirstName += myName.charAt(k);
System.out.println(myFirstName);
// if(myName.charAt(k))
// System.out.print(myFirstName);
}
int n;
for(int l = 0; l < otherName.length(); l++){
if(otherName.charAt(l) == ' '){
otherFirstName += otherName.charAt(0);
}
}
if(myName.compareTo(otherName) > 0)
result = 1;
else if(myName.compareTo(otherName) < 0)
result = -1;
else if(myLastName.compareTo(otherLastName) == 0){
if(myFirstName.compareTo(otherFirstName) > 0)
result = 1;
if(myFirstName.compareTo(otherFirstName) < 0)
result = -1;
}
return result;
}
public void printAllApplicantsByName(){
StringBuffer buf2 = new StringBuffer(
"\nScholarship = " + scholarship +
//" Amount that will be awarded among winners = " + getSingleScholarshipAmount() +
". Number of intended winners = " + intendedNumberAwarded);
//Arrays.sort(scholar, Comparator c);
buf2.append("\n------------------------------\n");
Scholar nullScholar = new Scholar("Jon Smith", 0.0, 0, 0, false);
int min;
int temp2 = 0;
int temp3 = 0;
int counter = 0;
Scholar temp;
Scholar [] temporary;
int countScan = 0;
int count2 = 0;
int count = 0;
for(int i = 0; i < scholar.length; i++){
if(scholar[i] instanceof Scholar)
count++;
//temp3 = i;
}
temporary = new Scholar[count];
for(int j = 0; j<temporary.length; j++){
if(scholar[j] != null)
temporary[j] = scholar[j];
}
//System.out.println("\n\n\n");
/*for(int i = 0; i < temporary.length; i++){
System.out.println(temporary[i]);
}*/
scholar = temporary;
// start iterating here, thanks
for (int index =0 ; index < scholar.length; index++)
{
min = index;
for(int scan = index + 1; scan < scholar.length; scan++){
if(compareNames(scholar[scan], scholar[min]) < 0){
min = scan;
// swap
temp = scholar[scan];
scholar[scan] = scholar[index];
scholar[index] = temp;
}
}
for(int i = 0; i < scholar.length; i++){
buf2.append ("[" + (i+1) + "]:" + scholar[i].getFullName() +
" scored " + scholar[i].getScore() + "\n");
}
System.out.println(buf2);
}
Thanks in advance.