I am having trouble with "//Clinton's delegates in order from highest to lowest". I realize that it's currently in ascending order (I was more familiar with this), but it still isn't looping enough times to even do the ascending order properly. I would like for the third column to be in descending order --> Integer.parseInt(primary[row][2]).
import java.io.*;
public static void main(String[] args) throws IOException
{
//Read text file
FileReader fr = new FileReader("primary1.txt");
BufferedReader br = new BufferedReader(fr);
//2D array
String[][] primary = new String[44][5];
//Section break
System.out.println("1. The file contents are:\n");
//Add column titles
System.out.println("States\t\t\t\tCandidate#1\tVotes\t\tCandidate#2\tVotes");
//Set delimiter as "/"
String line;
int i=0;
while((line=br.readLine())!=null){
primary[i]=line.split("/");
i++;
}
//Print text file
for(int row=0; row<primary.length; row++){
for(int col=0; col<primary[row].length; col++){
//Add space between columns
System.out.print(primary[row][col] + "\t\t");
}
//Newline
System.out.println("");
}
//Clinton's delegates in order from highest to lowest
int temp=0;
for(int row=0; row<primary.length-1; row++){
//Parse Integer
int delC = Integer.parseInt(primary[row][2]);
int delC1 = Integer.parseInt(primary[row+1][2]);
if(delC > delC1){
temp=delC1;
delC1=delC;
delC=temp;
}
System.out.println(temp);
}
}
Try this: