Hi there, I wanted to get the percentage of total vote per candidate but it always result to 0.00. Can anybody tell me what's wrong with my code?

    public static void main(String[] args) {

        //Array Declaration
        String[] candidate = {"Estrada J.", "Drilon F.", "Enrile J.", "Lacson"};
        int[] voteEs = {5, 5, 10, 11, 20, 30, 4, 15, 10, 30, 40, 50, 60};
        int[] voteDr = {10, 20, 30, 35, 25, 10, 10, 20, 30, 10, 20, 15, 30};
        int[] voteEn = {20, 15, 15, 20, 20, 30, 50, 50, 20, 10, 15, 15, 15};
        int[] voteLa = {50, 50, 10, 10, 10, 10, 10, 20 ,15, 30, 40, 40, 50};

        //Variable Declaration
        int ctr1, ctr2, totalVoteEs=0, totalVoteDr=0, totalVoteEn=0, 
                totalVoteLa=0, totalVotes;
        float percentEs, percentDr, percentEn, percentLa;
        String newPresident;
        System.out.println("************************************************"
                + "***********************************************************"
                + "*********");
        System.out.println("\t\t\t\t\t\tPRESIDENTIAL ELECTION 2016");
        System.out.println("************************************************"
                + "***********************************************************"
                + "*********");
        System.out.println("Candidates\t\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12"
                + "\tNCR");
        System.out.println("-----------------------------------------------"
                + "----------------------------------------------------------"
                + "-----------");

        for(ctr1=0; ctr1<candidate.length; ctr1++)
        {
        System.out.printf("%9s\t\t", candidate[ctr1]);
           if(candidate[ctr1].equals(candidate[0]))
           {
              for(ctr2=0; ctr2<voteEs.length; ctr2++) {
                  System.out.print(voteEs[ctr2] + "\t");
                  totalVoteEs = totalVoteEs + voteEs[ctr2]; }
                  System.out.print("\n");
           }//end if(candidate[0])
           else if(candidate[ctr1].equals(candidate[1]))
           {
               for(ctr2=0; ctr2<voteEs.length; ctr2++) {
                  System.out.print(voteDr[ctr2] + "\t");
                  totalVoteDr = totalVoteDr  + voteDr[ctr2]; }
               System.out.print("\n");
           }//end if(candidate[1]) loop
           else if(candidate[ctr1].equals(candidate[2]))
           {
               for(ctr2=0; ctr2<voteEs.length; ctr2++) {
                  System.out.print(voteEn[ctr2] + "\t");
                  totalVoteEn = totalVoteEn  + voteEn[ctr2]; }
               System.out.print("\n");
           }//end last candidate[]
           else 
               for(ctr2=0; ctr2<voteEs.length; ctr2++) {
                  System.out.print(voteLa[ctr2] + "\t");
                  totalVoteLa = totalVoteLa + voteLa[ctr2]; }


        }//end for(candidate) loop

               System.out.print("\n");


         System.out.println("************************************************"
                + "***********************************************************"
                + "*********");    


         totalVotes = totalVoteEs + totalVoteDr + totalVoteEn + totalVoteLa;
         percentEs = (totalVoteEs / totalVotes);
         percentDr = (totalVoteDr / totalVotes);
         percentEn = (totalVoteEn / totalVotes);
         percentLa = (totalVoteLa / totalVotes);



        System.out.println("CANDIDATE \t TOTAL VOTE \tPERCENTAGE");
        //total votes
        for(ctr1=0; ctr1 <candidate.length; ctr1++)
        {
         System.out.printf("%10s\t", candidate[ctr1]);
         if(candidate[ctr1].equals(candidate[0])) {
             System.out.printf("%7d\t\t%7.2f", totalVoteEs, percentEs);
             System.out.print("%\n");}
         else if(candidate[ctr1].equals(candidate[1])) {
             System.out.printf("%7d\t\t%7.2f", totalVoteDr, percentDr); 
             System.out.print("%\n");}
         else if(candidate[ctr1].equals(candidate[2])) {
             System.out.printf("%7d\t\t%7.2f", totalVoteEn, percentEn);
             System.out.print("%\n");}
         else {
             System.out.printf("%7d\t\t%7.2f", totalVoteLa, percentLa);
             System.out.print("%\n");}
        }
        System.out.println();
        System.out.println("OVERALL VOTES: " + totalVotes);




        //tally
       if((totalVoteEs > totalVoteDr) && (totalVoteEs > totalVoteDr) && (totalVoteEs> totalVoteLa))
           newPresident = candidate[0];
       else if((totalVoteDr > totalVoteEs) && (totalVoteDr > totalVoteEn) && (totalVoteDr > totalVoteLa))
           newPresident = candidate[1];
       else if((totalVoteEn> totalVoteEs ) && (totalVoteEn > totalVoteDr) && (totalVoteEn > totalVoteLa))
           newPresident = candidate[2];
       else
           newPresident = candidate[3];

        System.out.println("The Candidate with the highest vote is: " + newPresident);
        System.out.println("************************************************"
                + "***********************************************************"
                + "*********");  


run:
********************************************************************************************************************
                        PRESIDENTIAL ELECTION 2016
********************************************************************************************************************
Candidates      1   2   3   4   5   6   7   8   9   10  11  12  NCR
--------------------------------------------------------------------------------------------------------------------
Estrada J.      5   5   10  11  20  30  4   15  10  30  40  50  60  
Drilon F.       10  20  30  35  25  10  10  20  30  10  20  15  30  
Enrile J.       20  15  15  20  20  30  50  50  20  10  15  15  15  
   Lacson       50  50  10  10  10  10  10  20  15  30  40  40  50  
********************************************************************************************************************
CANDIDATE    TOTAL VOTE     PERCENTAGE
Estrada J.      290        0.00%
 Drilon F.      265        0.00%
 Enrile J.      295        0.00%
    Lacson      345        0.00%

OVERALL VOTES: 1195
The Candidate with the highest vote is: Lacson
********************************************************************************************************************

SAMPLE OUTPUT:

Recommended Answers

All 2 Replies

percentEs = (totalVoteEs / totalVotes);

totalVoteEs and totalVotes are both ints, so this division is done using integer arithmetic. Fractions are truncated to zero, so the result is zero. You can use a simple fudge like (1.0 * totalVoteEs / totalVotes) to force the calculation into floating point, or you could re-declare your variable(s) as floating point types

:) thank you so much for helping. I re-declared my variables and it fixed the problem :D I also forgot to multiply it by 100 silly me.

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.