I have been trying to compare objects in an Array by one if its properties so that I can sort the objects in the Array into a descending order. Here is the sample code: The array is Candidate[][]
System.out.println("How many positions for this election? > ");
numberOfPositions = sc.nextInt();
Candidate Candidate[][] = new Candidate[numberOfPositions][];
PoliticalParty Parties[][] = new PoliticalParty[numberOfPositions][];
for(int i=0;i<numberOfPositions;i++){
String name;
String politicalParty;
System.out.println("Enter position name > ");
position = sc.next();
System.out.println("How many seats? > ");
numberOfSeats = sc.nextInt();
System.out.println("How many candidates? > ");
numberOfCandidates = sc.nextInt();
Candidate[i] = new Candidate[numberOfCandidates+1];
Candidate[i].sort(votes); //<--------------------------This is what im trying//
Wherein (votes) is an int derived from a text file using this code:
System.out.println("Enter file name > ");
filename = sc.next();
try {
filescan = new Scanner(new File(filename));
} catch (FileNotFoundException ex) {
//Logger.getLogger(Election.class.getName()).log(Level.SEVERE, null, ex);
}
String L = System.lineSeparator();
filescan.useDelimiter(L);
while (filescan.hasNext()) {
numberOfVoters++;
line = filescan.next();
for(int x=0,j=0;j<line.length();j++){
switch(line.charAt(j)){
case ',':
x++;
break;
case ' ':
break;
default:
int y = line.charAt(j)-48;
//Integer.parseInt(line.charAt(j).toString());
Candidate[x][y].addVote();
break;
}
}
Wherein (vote) is encapsulated in another Class:
public class Candidate{
int votes = 0;
String politicalParty;
public Candidate(String name, String politicalParty) {
super(name);
this.politicalParty = politicalParty;
}
public void addVote() {
this.votes++;
//return votes;
}
public int getVotes() {
return votes;
}
@Override
public String getName() {
return getName();
}
public void displayFields(){
System.out.println(this.getName() + " (" + getPoliticalParty() + ") - " + votes);
}
public String getPoliticalParty() {
return politicalParty;
}
public void setPoliticalParty(String politicalParty) {
this.politicalParty = politicalParty;
}
}