I've scratched my head too long: anyone?
The snippet below is part of a larger program that accepts user names and scores as user input, then calculates a percentage. I've only included the relevant pieces here.
My trouble is at (line 32):
As is, the program compiles and runs fine, but all values in percent[j] come back as 0.0. If I replace "totalVotes" with a literal '100', it works fine.
In troubleshooting, i've verified values exist in votes[] and totalValue. The issue seems to simply be that it won't divide anything by totalValue! Ideas?
The snippet below is part of a larger program that accepts user names and scores as user input, then calculates a percentage. I've only included the relevant pieces here.
My trouble is at (line 32):
Code:
percent[j] = (votes[j] / totalVotes);
In troubleshooting, i've verified values exist in votes[] and totalValue. The issue seems to simply be that it won't divide anything by totalValue! Ideas?
Code:
import javax.swing.*;
public class Election {
//data fields
private int numCand;
private int totalVotes;
private String[] candidates;
private int[] votes;
private double[] percent;
// ~~constructor method.
public Election(int anumCand) {
numCand = anumCand;
candidates = new String[numCand]; //array for name of candidates
votes = new int[numCand]; //array for votes
percent = new double[numCand]; //array for percentage of votes
}
//~~This method will prompt user for a name and # of votes to fill each spot of the 'candidates' and 'votes' arrays
public void setVoteCounts() {
for(int i=0; i<numCand; i++) { //prompt user for names of candidates for # of candidates
candidates[i] = JOptionPane.showInputDialog("Enter a Name for Candidate # " + (i+1));
votes[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number of votes for " + candidates[i]));
totalVotes += votes[i];
}
}
//~~This method will calculate the total votes entered, determine each candidate %, and store the value in the 'percent' array
public void calcPercent() {
for(int j=0; j<numCand; j++) { //calculate percentage of vote for each candidate
percent[j] = votes[j] / totalVotes;
}
}
}