1. Member
Join Date
Mar 2011
Posts
26
Rep Power
0

my teacher give me a task and the task is i need to provide a voting system
but i just coded only the beginning.My problem is i don't know what's the code for counting the total number of votes voted for one person and what's the code to tally the votes and the code to know who the winner is.

Here is the code:

import java.io.*;

public class project{

public static void main(String[] args) throws IOException{
int a=0;
int b=0;
int vote[]=new int[50];
int num1=0;

System.out.println("Please enter 1 for Escobar and 2 for Yalung");

switch(num1){
case 1:
System.out.println("Escobar");
vote[a]=1;
break;

case 2:
System.out.println("Yalung");
vote[b]=1;
break;
}
System.out.println(name + "voted for " + num1);
}

}

2. You only need one array slot per runner, try incrementing the correct value each time someone votes. Of candidate 1 increment array[1], else increment item 2.

This will keep track of the votes, the winner is the higher of the two. Total votes is the sum of the two.

If you are allowed to use classes a oop approach would make this easier, have a class for candidate which keeps track of the votes internally

3. Member
Join Date
Mar 2011
Posts
26
Rep Power
0
I just added some code but t has the same problem

import java.io.*;

public class project{

public static void main(String[] args) throws IOException{
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int g=0;
int h=0;
int i=0;
int j=0;
int k=0;
int l=0;
int vote[]=new int[50];
int num1=0;
int num2=0;

System.out.println("Please enter 1 for Escobar and 2 for Yalung");

switch(num1){
case 1:
System.out.println("Escobar");
vote[a]=1;
break;

case 2:
System.out.println("Yalung");
vote[b]=1;
break;
}

System.out.println("Please enter 1 for Escobar and 2 for Yalung");

switch(num2){
case 1:
System.out.println("Ceniza");
vote[c]=1;
break;

case 2:
System.out.println("Atienza");
vote[d]=1;
break;
case 3:
System.out.println("alcones");
vote[e]=1;
break;

case 4:
System.out.println("mendoza");
vote[f]=1;
break;

case 5:
System.out.println("alcones");
vote[g]=1;
break;

case 6:
System.out.println("mendoza");
vote[h]=1;
break;

case 7:
System.out.println("alcones");
vote[i]=1;
break;

case 8:
System.out.println("mendoza");
vote[j]=1;
break;

case 9:
System.out.println("alcones");
vote[k]=1;
break;

case 10:
System.out.println("mendoza");
vote[l]=1;
break;
}

System.out.println(name + "voted for " + num1);
}

}

4. Member
Join Date
Mar 2011
Posts
94
Rep Power
0
I think some pseudocode might help you organize this better. For example:

Java Code:
```while we still want to use the voting program
if the user wants to vote
get the user's name
display candidates
get user's vote
increase the vote count for that candidate
display that candidate's name along with the user's name
else if the user wants to tally votes
count up all of the votes
display each candidate's name with their vote count
else if the user wants to quit
set a flag indicating that the user is done
else
display a message that the user has selected an invalid choice
end while```
I don't know if this is too complicated for what you need to do, but you get the idea.

5. Member
Join Date
Mar 2011
Posts
26
Rep Power
0
Yes that's what i need but the problem is i don't the codes

6. Converting pseudocode to real code isn't a huge leap.

7. Member
Join Date
Mar 2011
Posts
26
Rep Power
0
what's pseudocode by the way?

8. It's code that is an intermediary between English and code.

For an example, look a few posts above. Also, dont rule out making a
Candidate class.

9. Member
Join Date
Mar 2011
Posts
94
Rep Power
0
Here's some code to get you started:
Java Code:
```		boolean bKeepUsingProgram = true;
while (bKeepUsingProgram) {

switch (getUserChoice()) {
case 'v':
//process vote

break;
case 't':

break;
case 'q':
bKeepUsingProgram = false;
break;
default:
System.err.println("invalid choice");
break;
}
}
System.out.println("Thank you for using the voting program.");```
Like sunde stated, use your array to store the votes. When you're adding up votes for each candidate, you need to accumulate them. For example:
Java Code:
`vote[d] = vote[d] + 1;`
Make sure you initialize your array to all 0s first. When tallying the votes, loop through the array to find the index of the element that has the highest number of votes.

I like the OO approach too, but you may not be ready for that.

10. Say you want 10 candidates in your vote, lets create one slot of the array for each candidate.

Java Code:
`int[] candidates = new int[10];`
this array has 10 spots, 0 - 9.

So if the person votes for escobar, lets make him the 0 slot, increment it by one.

You can do
Java Code:
```candidates[0]++;

or

candidates[0] += 1;

or

candidates[0] = candidates[0] + 1;```
They are all the same.

the tally would require you to loop through the array and add each persons total votes to the tally, which would count all the votes placed.

The candidate with the highest amount of votes is the winner.

You can also try looking into using maps
Java Code:
`map<String, int>`
where the string would be the candidate name, and int would be how many votes they had.

Finally, the best approach to use would be OO, but it may be a bit too advanced for you, if it is not, give it a shot.

11. Member
Join Date
Mar 2011
Posts
26
Rep Power
0
Thanks for that i'll try tomorrow because it's almost 12 now here....i'll post the result here tomorrow but please reply to me...thank you so much...your great...thank you

12. Member
Join Date
Mar 2011
Posts
26
Rep Power
0
Hi, I think the code for adding one to the candidate works but the problem is I don't how will it appear like a tally of votes....By the way in my program the first witch is different on the second switch, I mean both of them are different category

Here's the new code, I edited the code above:

import java.io.*;

public class project{

public static void main(String[] args) throws IOException{
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int g=0;
int h=0;
int i=0;
int j=0;
int k=0;
int l=0;
int vote[]=new int[50];
int num1=0;
int num2=0;

//for looping
String ans2 = null;
String ans3 = "y";
String ans4 = null;
String ans5 = "y";

//for candidate slots
int[] candidates = new int[2];
int[] candidate = new int[10];

do{

System.out.println("Please enter 1 for Escobar and 2 for Yalung");

switch(num1){
case 1:
System.out.println("Escobar");
candidates[0]++;
break;

case 2:
System.out.println("Yalung");
candidates[1]++;
break;

default:
System.out.println("You didn't choose anyone");
break;
}

System.out.println(name + "voted for " + num2);

System.out.println(" COUNCILOR ");
System.out.println("Please enter 1 for Escobar ");
System.out.println("Please enter 4 for Mendoza ");
System.out.println("Please enter 5 for Fernandez ");
System.out.println("Please enter 6 for Maranan ");
switch(num2){
case 1:
System.out.println("Ceniza");
candidate[0]++;
break;

case 2:
System.out.println("Atienza");
candidate[1]++;
break;
case 3:
System.out.println("alcones");
candidate[2]++;
break;

case 4:
System.out.println("mendoza");
candidate[3]++;
break;

case 5:
System.out.println("Fernandez");
candidate[4]++;
break;

case 6:
System.out.println("Maranan");
candidate[5]++;
break;

case 7:
System.out.println("Lano");
candidate[6]++;
break;

case 8:
System.out.println("Dolor");
candidate[7]++;
break;

case 9:
candidate[8]++;
break;

case 10:
System.out.println("Axalan");
candidate[9]++;
break;

default:
System.out.println("You didn't choose anyone");
break;
}

System.out.println(name + "voted for " + num2);

System.out.println("Continue voting for councilors [Y / N]:");

}
while(ans4.equalsIgnoreCase(ans5));

}
}

13. Member
Join Date
Mar 2011
Posts
26
Rep Power
0
I already did the tally.The problem now is how i can get 1 winner for captain and 8 winner for councilor....

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•