error when testing program (for loop with nested if statement))
Hi I'm trying to write a program that will request a number of candidates names and number of votes they've got and at the end output the candidate with the highest number of votes,when I've run this program it is giving me the correct output if the first candidate or the second candidate has the highest number of votes but not if the third (ie last candidate,no matter how many candidates are entered) has,would appreciate any help with this it's probably just something small
Code:
package electioncandidates;
import javax.swing.JOptionPane;
public class ElectionCandidates {
public static void main(String[] args) {
String strInput,
topCand = null,strInput2,strTemp;
int vote=0,topVote=0,temp=0;
strInput = JOptionPane.showInputDialog("What is candidates name");
strInput2=JOptionPane.showInputDialog("What is candidates mark?");
vote=Integer.parseInt(strInput2);
for(int i=1;i<3;i++){
if(vote>topVote)
{
temp = vote;
vote=topVote;
topVote=temp;
strTemp=strInput;
strInput=topCand;
topCand=strTemp;}
strInput = JOptionPane.showInputDialog("What is candidates name");
strInput2=JOptionPane.showInputDialog("What is candidates mark?");
vote=Integer.parseInt(strInput2);}
JOptionPane.showMessageDialog(null,"The top candidate is "+topCand+" with "+topVote+"votes");
}
}