Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: No Percentage?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default No Percentage?

    please check this one please...
    the program is not computing for the percentage...

    public class PollVotesGUI_UF {
     
        private static DecimalFormat df = new DecimalFormat("000.000");
     
        public static void main(String[] args) {
     
            String[] votes = {"First", "Second", "Third", "Fourth", "Result", "Quit"};
     
            int first = 0,
                second = 0,
                third = 0,
                fourth = 0;
     
            int totalVotes = 0;
     
            double firstPercent = 0.0,
                   secondPercent = 0.0,
                   thirdPercent = 0.0,
                   fourthPercent = 0.0;     
     
            while (true) {
     
                int voteSelection = JOptionPane.showOptionDialog(null, "Press Your Vote :", "Vote.", JOptionPane.ERROR_MESSAGE,
                                                                 JOptionPane.INFORMATION_MESSAGE, null, votes, "");
     
                switch (voteSelection) {
     
                    case 0:
     
                        first++;
                        break;
     
                    case 1:
     
                        second++;
                        break;
     
                    case 2:
     
                        third++;
                        break;
     
                    case 3:
     
                        fourth++;
                        break;
     
                    case 4:                 
     
                        if (totalVotes == 0) {
     
                            JOptionPane.showMessageDialog(null, "No Votes Accumulated.", "No Votes.", JOptionPane.WARNING_MESSAGE);
                        }
                        else if (totalVotes > 0) {
     
                            firstPercent = (first / totalVotes) * 100;
                            secondPercent = (second / totalVotes) * 100;
                            thirdPercent = (third / totalVotes) * 100;
                            fourthPercent = (fourth / totalVotes) * 100;
     
                            JOptionPane.showMessageDialog(null, "Total Votes : " + totalVotes +
                                                          "\n" + "\n" + 
                                                          "Poll A Has  " + df.format(firstPercent) + " % " + " Of Total Votes.");
                        }                    
     
                        JOptionPane.showMessageDialog(null, "Thank You For Voting.");
                        System.exit(0);
                        break;
     
                    case 5:
     
                        JOptionPane.showMessageDialog(null, "Thank You.");
                        System.exit(0);
                        break;
     
                    case -1:
     
                        JOptionPane.showMessageDialog(null, "No Votes Will Be Finilized Due To Exiting The Program Forcely.",
                                                      "System Message.", JOptionPane.WARNING_MESSAGE);
                        System.exit(0);
                        break;
     
                    default:
     
                        JOptionPane.showMessageDialog(null, "Unexpected Value.");
                        System.exit(0);
                        break;
                }
     
                totalVotes = first + second + third + fourth;
     
                JOptionPane.showMessageDialog(null, "" + "\n" + "First : " + first + 
                                              "\n" + "Second : " + second + 
                                              "\n" + "Third : " +  third +
                                              "\n" + "Fourth : " + fourth +
                                              "\n" + "\n" + "Current Votes : " + totalVotes);
            }
        }
    }

    but if i press the first or any of them ONCE...
    the program says... Poll <> has 100% votes


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: No Percentage?

    100%? It should be saying 0%... The reason is you're using integer mathematics with truncating decimals. This means that anytime a number evaluates to something that has a decimal, Java will automatically remove the decimal part. Ex:

    1.3 becomes 1
    1.999999 becomes 1
    3 / 5 becomes 0

    So, when you're doing some division with integers, put an explicit cast to double (unless you want the rounding off, it's quite useful sometimes).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 18th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: No Percentage?

    but in this one...
    public class PollVotes {
     
        private static DecimalFormat df = new DecimalFormat("0.00");
        private static DecimalFormat df2 = new DecimalFormat("0");
        private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
        public static void main(String[] args) throws IOException {
     
            double pollA,
                   pollB,
                   pollC,
                   pollD;
     
            double totalVotes,
                   totalPercentage;
     
            double pollAPercent = 0.0,
                   pollBPercent = 0.0,
                   pollCPercent = 0.0,
                   pollDPercent = 0.0;
     
            System.out.print("Voting Is Now Open");
            System.out.println("\n");
     
            System.out.print("Poll A: ");
            pollA = Integer.parseInt(br.readLine());
     
            System.out.print("Poll B: ");
            pollB = Integer.parseInt(br.readLine());
     
            System.out.print("Poll C: ");
            pollC = Integer.parseInt(br.readLine());
     
            System.out.print("Poll D: ");
            pollD = Integer.parseInt(br.readLine());
     
            totalVotes =  pollA + pollB +  pollC + pollD;
     
            pollAPercent = (pollA / totalVotes) * 100;
            pollBPercent = (pollB / totalVotes) * 100;
            pollCPercent = (pollC / totalVotes) * 100;
            pollDPercent = (pollD / totalVotes) * 100;
     
            totalPercentage = pollAPercent + pollBPercent + pollCPercent + pollDPercent;
     
            System.out.println("\n");
            System.out.println("Poll A Has " + df.format(pollAPercent) + "% Of Total Votes.");
            System.out.println("Poll B Has " + df.format(pollBPercent) + "% Of Total Votes.");
            System.out.println("Poll C Has " + df.format(pollCPercent) + "% Of Total Votes.");
            System.out.println("Poll D Has " + df.format(pollDPercent) + "% Of Total Votes.");
            System.out.println("Total Votes: " + df2.format(totalPercentage) + "%");
        }
    }

    i dont get any problems regarding with computation....

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: No Percentage?

    I've moved this thread to 'what's wrong with my code'
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. The Following User Says Thank You to JavaPF For This Useful Post:

    chronoz13 (December 18th, 2009)

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: No Percentage?

    Yep, that's because in that second bit of code you declared the variables as doubles to begin with, and as such automatically get the use of double precision in all calculations.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 18th, 2009)

  9. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: No Percentage?

    ahh its because of the flow of the program... tnx for that world.. im relived a bit ...