Im only starting arrays and im stuck on the percentage of the total votes. I have all the rest of it compiling Im just getting an error when I try with the percentage bit. I havent finished the last output as I know how to that just want to get the percentage working. If someone could please help would be greatly appreciated!!
Error: cannot find symbol
symbol: variable i
location: class Election
This is what I have been asked to do:
Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, the votes received by that candidate and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.
Hint: use parallel arrays
A sample output is:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Murphy 1800 9.33
Total 19300
The winner of the election is Duffy
This is my code:
import java.util.Scanner; class Election { public static void main(String args[]) throws Exception { Scanner in = new Scanner(System.in); String[] candidate = new String[5]; int[] votes = new int[5]; int currentCandidate = 0; int sum = 0; while(currentCandidate < candidate.length) { System.out.println("Please enter candidates last name: "); candidate[currentCandidate] = in.next(); currentCandidate++; } for(int i = 0; i < candidate.length; i++) { System.out.println("Please enter the total votes for " + candidate[i]); votes[i] = in.nextInt(); sum += votes[i]; } for (int j = 0; j < votes.length; j++) { double percentage = (double) votes[i] / (double) sum * 100; } int max = votes[0]; for (int k = 0; k < votes.length; k++) { if(votes[k] > max) max = votes[k]; } System.out.println(" The result of the election is:" + "\n"); System.out.println(" Candidate " + " Votes " + " % of Total Votes " + "\n"); } }