1

So I'm having trouble with my printResults method in my TestCandidate class. Everything seems to run as expected, except for the column displaying the percent of total votes each candidate received. For some reason, that operation is only printing 0's. (For the record, when i use the word "operation," I'm mainly referring to the list[i].getVotes / getTotal(list) operation. Just to clarify).

I've tried various combinations of parenthesizing the list[i].getVotesmethod and the getTotal(list) method but to no avail. I've also tried performing the operation outside of the print statement and just printing a variable that the operation would have been assigned to.

The strange thing is, when I take either the list[i].getVotes or the getTotal(list) out and just leave one, they both print fine. Just not together and I just can't figure out why.

So, in summary, why won't list[i].getVotes / getTotal(list) divide properly? (Please do not try to do anything but what the question asks for, I'd like to learn and fix my own mistakes and best optimize my code as much as possible but I simply cannot get past this one for whatever reason). Thank you!

Here's the Candidate class:

public class Candidate
{       
    private int votes;
    private String candidateName;

    public Candidate(String name, int numVotes)
    {
        votes = numVotes;
        candidateName = name;       
    }
    public String getName()
    {
        return candidateName; 
    }
    public void setName(String name)
    {
        candidateName = name;
    }
    public int getVotes()
    {
        return votes;
    }
    public void setVotes(int numVotes)
    {
        votes = numVotes;
    }       
    public String toString()
    {
        return candidateName + " received:\t" + votes + " votes."; 
    }
}

Here's the TestCandidate class:

 public class TestCandidate 
    {       
       public static void main(String[] args)
        {                             
          Candidate[] election = new Candidate[5];

        election[0] = new Candidate("John Smith", 5000);
        election[1] = new Candidate("Mary Miller", 4000);
        election[2] = new Candidate("Michael Duffy", 6000);
        election[3] = new Candidate("Tim Robinson", 2500);
        election[4] = new Candidate("Joe Ashtony", 1800); 

        printVotes(election);
      System.out.println();
      printResults(election);
      System.out.println();
        System.out.println("The total amount of votes cast was: " + getTotal(election) + ".");                                      
    }
    public static <E> void printVotes(E[] array)    //prints the candidates and the amount of votes they 
    {                                                           //each received
        for(E element : array)
        {
            System.out.printf("%s ", element);
            System.out.println();
        }
    }
    public static int getTotal(Candidate[] list)    //adds up the votes from each candidate and prints the
    {                                                           //total amount
        int totalVotes = 0;
        for(int i = 0; i < list.length; i++)
        {
            totalVotes += list[i].getVotes();
        }
        return totalVotes;
    }   
   public static void printResults(Candidate[] list)  //prints a table containing candidate's name, amount of votes, and the 
   {                                                  //percentage of total votes they earned
      System.out.println("Candidate: \t Votes Recieved: \t Percent of Total Votes:");      
      for(int x = 0; x < list.length; x++)
      {                       
         System.out.println(list[x].getName() + " \t " + list[x].getVotes() + " \t \t \t " + list[x].getVotes()/getTotal(list) + "%");                
      }
   }                    
}

And here's my current output:

John Smith received:    5000 votes. 
Mary Miller received:   4000 votes. 
Michael Duffy received: 6000 votes. 
Tim Robinson received:  2500 votes. 
Joe Ashtony received:   1800 votes. 

Candidate:   Votes Recieved:     Percent of Total Votes:
John Smith       5000            0%
Mary Miller      4000            0%
Michael Duffy    6000            0%
Tim Robinson     2500            0%
Joe Ashtony      1800            0%

The total amount of votes cast was: 19300.
  • 1
    possible duplicate of [Division of integers in Java](http://stackoverflow.com/questions/7220681/division-of-integers-in-java) – Radiodef Mar 28 '15 at 18:34

3 Answers3

3

A decimal representation of a fraction is a number between 0 and 1, which an integer cannot represent. Also, it's not a percentage because you haven't multiplied by 100.

You can multiply by 100 before you divide, or you can cast the bottom of the fraction to (double) to preserve the decimal places before you multiply by 100.

Working with int may be risky for large numbers - suppose a candidate got 30 million votes, multiplying by 100 would wrap it around into a negative number because int can't represent 3 billion.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
2

enter image description here

(double)list[x].getVotes()/getTotal(list)*100
Prashant Ghimire
  • 4,890
  • 3
  • 35
  • 46
2

to get the percentages to show up correctly use the following line of code:

(double)100*list[x].getVotes()/getTotal(list)

this happens because Java integers division gives you all these zeros that you see in your results (0%):

adrCoder
  • 3,145
  • 4
  • 31
  • 56