I am working on the following Java application

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. 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
Anthony 1800 9.33
Total 19300

The Winner of the Election is Duffy.


This is the code that I have so far. I got it to compile and print into the correct format. I am having trouble getting the program to allow the user to input the candidates and the votes received. Can someone please help

import java.util.*;
import java.text.NumberFormat;

public class Election
{
  public static void main(String[] args)
  {
  
    String[] names = new String [5];
    
    int[] votes = new int[5];
    
	 String winner = "";
    int winnerVotes = 0;
    int totalVotes = 0;
    int j;
    
    for (j = 0; j< votes.length; j++)
    {
      totalVotes += votes[j];
    }
    
    NumberFormat formatter = NumberFormat.getNumberInstance();
    formatter.setMinimumIntegerDigits(1);
    formatter.setMaximumFractionDigits(2);
    
    System.out.println("\nCandidate Votes Received % of Total Votes");
    System.out.println("--------- -------------- ----------------");
    
    for (j = 0; j< names.length; j++)
    {
      if (votes[j]>winnerVotes)
            {
        winner = names[j];
        winnerVotes = votes[j];
             }
          System.out.println(names[j] + "\t\t" + (int)votes[j] + "\t\t" + (formatter.format(((double)votes[j]/totalVotes)*100)));
     }
            System.out.println("Total " + totalVotes);
          System.out.println("The winner of the election is " + winner);
          }//main
}//election
import java.util.*;
import java.text.NumberFormat;

public class Election2
{
	public static void main(String[] args)
	{
		Scanner kb = new Scanner(System.in); // declaring/defining a Scanner object and giving it a handle to the standard input stream
		String[] names = new String [5]; // empty string array declaration/definition, stores 5 Strings

		short[] votes = new short[5]; // empty short-int array declaration/definition, stores 5 short-ints

		for(short i = 0; i < 5; i++) // doing something 5 times
		{
			Exception exp = null; // temp Exception object, pointing to null for now

			do{
			    try{
                                       exp = null;
					System.out.print("Enter the name of Candidate: " + (i+1) + " "); // request for input
					names[i] = kb.next(); // Scanner blocks - waits for input from user
					System.out.print("Enter the total votes associated with Candidate: " + (i+1) + " "); // request for input
					votes[i] = kb.nextShort(); // scanner block - waits for input from user
				}catch(Exception e){exp = e;}
			}
			while(names[i] == null || exp != null);
		}

		String winner = ""; // String data-type placeholder for the winner
		short winnerVotes = 0; // votes the winner has
		short totalVotes = 0; // total votes accumulated
		short j; // declared short-int named j, not defined

		for (j = 0; j< votes.length; j++) // doing something 5 times...
		{
			totalVotes += votes[j]; // summing the votes... !
		}

		NumberFormat formatter = NumberFormat.getNumberInstance(); // declaring/defining a NumberFormat instance named formatter
		formatter.setMinimumIntegerDigits(1); // ? not too familiar with these commands but they seem self-explanatory
		formatter.setMaximumFractionDigits(2);

		System.out.println("\nCandidate Votes Received % of Total Votes");
		System.out.println("--------- -------------- ----------------");

		for (j = 0; j< names.length; j++) // doing something 5 times
		{
			if (votes[j]>winnerVotes) // if the vote in index j of vote array is greater than winner votes..
			{
				winner = names[j]; // store the name associated with the vote in the String winner
				winnerVotes = votes[j]; // store the vote in the int winnerVotes
			}
			System.out.println(names[j] + "\t\t" + (short)votes[j] + "\t\t" + (formatter.format(((double)votes[j]/totalVotes)*100)));
		} // ^^ printing stuff out

		System.out.println("Total " + totalVotes); // the total votes,
		System.out.println("The winner of the election is " + winner); // printing out hte winner!
	}//main
}//election
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.