Register FAQ Calendar Search Today's Posts Mark Forums Read

Go Back   Java Programming Forums > Java Programming > New to Java

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2008, 12:17 AM
Junior Member
 
Join Date: Nov 2008
Posts: 2
loser321123 is on a distinguished road
Default please help !!!!!!!

ok so im making this program. it lets the user enter a number of candidates, then their names, then how many votes each candidate received.

i want the program to out put the total number of votes. how much each candidate received and who the winner is. i also want it to show what percentage of the total votes each candidate received.


i need help with the precentage part and i also need help with the displaying part.

below is a copy of my code . . . . in bold is the output line of code. i need help writing a long of code that has everything that i mentioned above in it.

thanks . . .

Java Code:
 
 
package test1;
import javax.swing.*;
 
public class Test1 {
 
 
    public static void main(String[] args) {
 
        int winIND;
        int numCand;        //number of candidates 
        int numVotes;        //number of votes
        int best = 0;        //highest number of votes
        int voteTotal;        //total number of votes
        //int total;        //sum of votes
        String strName;        //name of candidate
        String strNumVote;    //number of votes for each candidate
        String strNumCand;    // get the number of candidates from user
 
 
 
        //get the number of candidates
        strNumCand = JOptionPane.showInputDialog("Please Enter Number of Candidates: ");
        numCand = Integer.parseInt(strNumCand);
 
 
        int votes[] = new int[numCand];
 
 
 
        for (int i=0; i<numCand; i++) {
            strName = JOptionPane.showInputDialog("Enter Name of Candidate: ");
 
 
            strNumVote = JOptionPane.showInputDialog("Please enter total number of votes: ");
            votes[i] = Integer.parseInt(strNumVote);
 
 
            int total = total + votes[i];
 
            if (votes[i] > best)
            best = votes[i];
 
 
        }
        int total = sumVotes(votes);
        int k = winIndex(votes);
 
 
        JOptionPane.showMessageDialog(null, "Total votes: " + total + "/n The winner is: " + strName[k]);

 
    }
 
 
    public static int sumVotes(int a[]) {
        int sum = 0;
        for (int j = 0; j<a.length; j++)
 
        {
 
            sum += a[j];
        }
 
            return sum;
    }
    public static int winIndex(int votes[]) {
 
        int winIND = 0;
            for(int i = 0; i<votes.length; i++)
            {
                if(votes[i]>votes[winIND]);
                    winIND = i;
            }
 
        return winIND;
 
    }
 
}
Reply With Quote
  #2 (permalink)  
Old 06-11-2008, 12:17 AM
Junior Member
 
Join Date: Nov 2008
Posts: 2
loser321123 is on a distinguished road
Default Re: please help !!!!!!!

as of now there is an error on strName[k]
Reply With Quote
  #3 (permalink)  
Old 06-11-2008, 10:50 AM
JavaPF's Avatar
Java Programmer
 
Join Date: May 2008
Location: Deep inside the Eclipse IDE
Posts: 172
JavaPF will become famous soon enoughJavaPF will become famous soon enough
Default Re: please help !!!!!!!

Hello and welcome to the Java Programming Forums.

You were very close to making this code work. I have made a few changes for you:

Java Code:
package test1;
import javax.swing.*;
public class Test1 {
 
 public static int total;
 
    public static void main(String[] args) {
 
        int winIND;
        int numCand;        //number of candidates 
        int numVotes;        //number of votes
        int best = 0;        //highest number of votes
        int voteTotal;        //total number of votes
 
        String strName = null;        //name of candidate
        String strNumVote;    //number of votes for each candidate
        String strNumCand;    // get the number of candidates from user
 
        //get the number of candidates
        strNumCand = JOptionPane.showInputDialog("Please Enter Number of Candidates: ");
        numCand = Integer.parseInt(strNumCand);
 
        int votes[] = new int[numCand];
 
        for (int i=0; i<numCand; i++) {
 
            strName = JOptionPane.showInputDialog("Enter Name of Candidate: ");
            strNumVote = JOptionPane.showInputDialog("Please enter total number of votes: ");
 
            votes[i] = Integer.parseInt(strNumVote);
 
            total = total + votes[i];
 
            if (votes[i] > best)
 
            best = votes[i];
         }
 
       total = sumVotes(votes);
       int k = winIndex(votes);
 
        JOptionPane.showMessageDialog(null, "Total votes: " + total + " and the winner is: " + strName + " with " + best + " votes.");
 
    }
 
 
    public static int sumVotes(int a[]) {
 
     int sum = 0;
        for (int j = 0; j<a.length; j++)
         {
             sum += a[j];
        }
             return sum;
    }
 
    public static int winIndex(int votes[]) {
 
        int winIND = 0;
            for(int i = 0; i<votes.length; i++)
            {
                if(votes[i]>votes[winIND]);
                    winIND = i;
            }
 
        return winIND;
 
    }
}
You cannot have strName[k] because you have not assigned strName as an array.

This displays total number of votes, who the winner is and how many votes they received.

If you want to display each candidate and also show their percentage then you are going to have to work this into a for loop:

Java Code:
JOptionPane.showMessageDialog(null, "Total votes: " + total + " and the winner is: " + strName + " with " + best + " votes.");
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Custom Search
100 most searched terms
Search Cloud
"bad endpoint type" com.sun.xml.internal.messaging.saaj.soapexceptionimpl com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed convert arraylist to map date_format_now eclipse shortcut keys ejb3 quartz java convert double to binary java forum java forums java jtextarea bold java jtextarea color java jtextarea font java jtextarea font size java programmer forum java programmers forum java programming forum java programming forums java read last line java read last line of file java sendkeys java.security.privilegedactionexception java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad response java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed javapf javaprogrammingforums jtextarea bold jtextarea font jtextarea font color jtextarea font size programing forums programming forums reset jcombobox saaj0008 saaj0008: bad response; bad request saaj0008: bad response; not found severe: saaj0008 severe: saaj0008: bad response; bad request severe: saaj0008: bad response; not found soap java.security.privilegedactionexception soapexceptionimpl: bad endpoint type textpad java

All times are GMT. The time now is 12:32 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.