Description of what the program should do:
- The program should allow 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 the candidate and the percentage of the total votes received by the candidate.
- The program should also output the winner of the election.
- If the user enters a negative value for the number of votes received for a candidate then the program should set the value of vote for that candidate to zero.
- If the user enters a last name in small letters the program should display the surname with the first character in capitals.
Here a Sample of Input and Output:
****values and names enter by the user is highlighted in green****
Enter candidate's name and the votes received by the candidate
Jack
5000
Enter candidate's name and the votes received by the candidate
Mike
4000
Enter candidate's name and the votes received by the candidate
Joe
6000
Enter candidate's name and the votes received by the candidate
Robin
2500
Enter candidate's name and the votes received by the candidate
Tony
1800
Candidate Votes Received % of Total Votes
Jack 5000 25.906735751295333
Mike 4000 20.72538860103627
Joe 6000 31.088082901554404
Robin 2500 12.953367875647666
Tony 1800 9.32642487046632
Total 19300
The Winner of the Election is Joe.
My Code so far for this task:
import java.util.Scanner; public class voting { public static void main(String[] args) { int totalVotes = 0; float percentOfVotes; voting myTest = new voting(); String[] enteredNames = myTest.readNames(); int[] enteredVotes = myTest.readVotes(); for ( int i=0; i < enteredVotes.length; i++ ) { totalVotes += enteredVotes[ i ]; } System.out.println("Total" + " " + totalVotes); for( int i = 0; i < enteredVotes.length; i++) { percentOfVotes = ((enteredVotes[i]/totalVotes)*100); System.out.println(enteredNames[i]+ " " + enteredVotes[i] + " " + percentOfVotes ); } } public String[] readNames() { Scanner in = new Scanner(System.in); String names[] = new String[5]; for (int i = 0; i < names.length; i++) { System.out.println("Enter candidate's name"); String inputWord1 = in.next(); String firstLetter = inputWord1.substring(0,1); [COLOR="Lime"]// Get first letter[/COLOR] String remainder = inputWord1.substring(1); [COLOR="Lime"]// Get remainder of word.[/COLOR] String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase(); names[i] = capitalized; } return ( names ); } private int[] readVotes() { int vote = 0; int[] votes1 = new int[5]; for (int i = 0; i < votes1.length; i++) { System.out.println("Enter candidate's votes"); vote = EasyIn.getInt(); votes1[i]= vote; if (votes1[i]<0) { votes1[i]=0; } } return ( votes1); } }
My Sample of Input and Output for my code above:
****values and names enter by the user is highlighted in green****
Enter candidate's name
Alice
Enter candidate's name
john
Enter candidate's name
petEr
Enter candidate's name
thomas
Enter candidate's name
jerry
Enter candidate's votes
1293
Enter candidate's votes
1283
Enter candidate's votes
-923
Enter candidate's votes
-128
Enter candidate's votes
0
Total 2576
Alice 1293 0.0 //having trouble with the percentage of votes recieved.
John 1283 0.0
Peter 0 0.0
Thomas 0 0.0
Jerry 0 0.0
I have tried all that I can and now I need help for this community to fix the code.
Thank you in advance.
Java'88.