Results 1 to 20 of 35
Thread: need help
- 03-24-2011, 02:29 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
need help
Hi i have my system here and i don't know how to get one winner for captain and 8 winners for councilor...
Here's the code:
import java.io.*;
public class project{
public static void main(String[] args) throws IOException{
int vote[]=new int[50];
int num1;
int num2;
//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];
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = input.readLine();
do{
System.out.println("Please enter 1 for Escobar and 2 for Yalung");
num1 = Integer.parseInt(input.readLine());
switch(num1){
case 1:
System.out.println(name+" voted Escobar as the Brgy.Captain");
candidates[0]++;
break;
case 2:
System.out.println(name+" voted Yalung as the Brgy. Captain");
candidates[1]++;
break;
default:
System.out.println("You didn't choose anyone");
break;
}
do{
System.out.println(" COUNCILOR ");
System.out.println("Please enter 1 for Escobar ");
System.out.println("Please enter 2 for Yalung");
System.out.println("Please enter 3 for Alcones");
System.out.println("Please enter 4 for Mendoza ");
System.out.println("Please enter 5 for Fernandez ");
System.out.println("Please enter 6 for Maranan ");
System.out.println("Please enter 7 for Lano");
System.out.println("Please enter 8 for Dolor");
System.out.println("Please enter 9 for Aranzado");
System.out.println("Please enter 10 for Axalan");
num2 = Integer.parseInt(input.readLine());
switch(num2){
case 1:
System.out.println( name+" voted Ceniza as the councilor");
candidate[0]++;
break;
case 2:
System.out.println(name+" voted Atienza as the councilor");
candidate[1]++;
break;
case 3:
System.out.println(name+" voted alcones as the councilor");
candidate[2]++;
break;
case 4:
System.out.println(name+" voted mendoza as the councilor");
candidate[3]++;
break;
case 5:
System.out.println(name+" voted Fernandez as the councilor");
candidate[4]++;
break;
case 6:
System.out.println(name+" voted Maranan as the councilor");
candidate[5]++;
break;
case 7:
System.out.println(name+" voted Lano as the councilor");
candidate[6]++;
break;
case 8:
System.out.println(name+" voted Dolor as the councilor");
candidate[7]++;
break;
case 9:
System.out.println(name+" voted Aranzado as the councilor");
candidate[8]++;
break;
case 10:
System.out.println(name+" voted Axalan as the councilor");
candidate[9]++;
break;
default:
System.out.println(name+" You didn't choose anyone");
break;
}
System.out.println(name + "voted for " + num2);
System.out.println("Continue voting for councilors [Y / N]:");
ans2 = input.readLine();
}while(ans2.equalsIgnoreCase(ans3));
System.out.println("New Voter? [Y / N]:");
ans4 = input.readLine();
}
while(ans4.equalsIgnoreCase(ans5));
System.out.println(" Tally of results: ");
System.out.println("Brgy Captain:");
System.out.println("Escobar: " +candidates[0]);
System.out.println("Yalung: " +candidates[1]);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("For Councilors:");
System.out.println("Ceniza : "+candidate[0]);
System.out.println("Atienza : "+candidate[1]);
System.out.println("Alcones : "+candidate[2]);
System.out.println("Mendoza : "+candidate[3]);
System.out.println("Fernandez: "+candidate[4]);
System.out.println("Maranan : "+candidate[5]);
System.out.println("Lano : "+candidate[6]);
System.out.println("Dolor : "+candidate[7]);
System.out.println("Aranzado : "+candidate[8]);
System.out.println("Axalan : "+candidate[9]);
}
}
- 03-24-2011, 02:51 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
I'm not sure where to start. Your code is hard to follow. But I will say that this statement is doing you no good:
What are you trying to accomplish with that? If the condition is true, there are no statements to execute.Java Code:while(ans4.equalsIgnoreCase(ans5));
Also, instead of this line:
It's clearer to write it like this:Java Code:}while(ans2.equalsIgnoreCase(ans3));
...unless you're planning to change user responses to something else.Java Code:}while(ans2.equalsIgnoreCase("y"));
- 03-24-2011, 03:08 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
yes i think that's right but how can i get the 8 winners for councilor?
- 03-24-2011, 03:28 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
If you're referring to the top 8 winners, you could loop through your candidate array and find the indexes of the two lowest vote getters. In the end, the other 8 indexes are the top 8 winners.
- 03-25-2011, 03:11 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
can you give a sample code....please...
- 03-25-2011, 04:04 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That will find the lowest, when you find the lowest, add it to the loser array, and remove it from the candidates, repeat the process again. There may be other, better ways, take some time and think, maybe you will come up with something.Java Code:int[] losers = new int[2]; int min = candidates[0]; for(int i = 0; i < candidates.length; i++){ if(candidates[i] < min){ min = candidates[i]; } }
- 03-25-2011, 08:46 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
- 03-25-2011, 08:47 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ya, if you remove the person from the list the next loop through will find the second lowest person, and remove them. Leaving you with the 8 winners.
- 03-25-2011, 08:54 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
ok I'll try it but you know what your code works, it get the lowest number of votes
- 03-25-2011, 08:56 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
In your instructions will i just edit your code sample or add another set of code?
- 03-25-2011, 09:21 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
is this what you mean:
int[] losers = new int[2];
int m;
int min = candidate[0];
for(int i = 0; i < candidate.length; i++){
if(candidate[i] < min){
min = candidate[i];
m=losers[2]-min;
- 03-25-2011, 09:52 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
that's not quite right, you could simply run my loop(which can definitely be improved on, it's a quick and dirty example) twice(or use a nested loop). Which is really not too great of a choice. A better choice would be to loop through once and find the two lowest numbers. This would require a bit more logic, and if else clauses because you will be juggling 2 smallest numbers.
- 03-25-2011, 10:25 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
- 03-25-2011, 10:30 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
yes, you can, and you should.
- 03-25-2011, 10:49 AM #15
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
I tried it and there's an error that says like this:
'else' without 'if'
here's the code:
if(candidate[0] > candidate[1] || candidate[0] > candidate[2] || candidate[0] > candidate[3] || candidate[0] > candidate[4] || candidate[0] > candidate[5] || candidate[0] > candidate[6] || candidate[0] > candidate[7] || candidate[0] > candidate[8] || candidate[0] > candidate[9]);
{
System.out.println("dklfjl");
}
else
{
System.out.println("")
}
- 03-25-2011, 10:52 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
no, do not do that, that is a bad approach. You should try not to ever hard code an array loop. What happens if the array gets larger or smaller? Always use a loop for looping through an array.
- 03-25-2011, 10:56 AM #17
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
You have two syntax errors:
Java Code:if(candidate[0] > candidate[1] || candidate[0] > candidate[2] || candidate[0] > candidate[3] || candidate[0] > candidate[4] || candidate[0] > candidate[5] || candidate[0] > candidate[6] || candidate[0] > candidate[7] || candidate[0] > candidate[8] || candidate[0] > candidate[9]); // <---- do NOTput a semi-colon here { System.out.println("dklfjl"); } else { System.out.println("") // <--- missing semi-colon here }
- 03-25-2011, 12:07 PM #18
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
- 03-25-2011, 12:10 PM #19
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
How much do you know about loops? If you don't know much I suggest you read up on them. They are ideal for arrays and other containers.
I suggest you practice looping through and finding the minimum value in an array with a loop. Start small and work up.
- 03-25-2011, 12:10 PM #20
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks