Page 1 of 2 12 LastLast
Results 1 to 20 of 35

Thread: need help

  1. #1
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default 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]);

    }
    }

  2. #2
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    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:
    Java Code:
    while(ans4.equalsIgnoreCase(ans5));
    What are you trying to accomplish with that? If the condition is true, there are no statements to execute.

    Also, instead of this line:
    Java Code:
    }while(ans2.equalsIgnoreCase(ans3));
    It's clearer to write it like this:
    Java Code:
    }while(ans2.equalsIgnoreCase("y"));
    ...unless you're planning to change user responses to something else.

  3. #3
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    yes i think that's right but how can i get the 8 winners for councilor?

  4. #4
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    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.

  5. #5
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    can you give a sample code....please...

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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];
      }
    }
    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.

  7. #7
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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];
      }
    }
    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.

    That will only find the lowest if i follow your next instruction, will I find who the winner is?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  9. #9
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    ok I'll try it but you know what your code works, it get the lowest number of votes

  10. #10
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    In your instructions will i just edit your code sample or add another set of code?

  11. #11
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    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;

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  13. #13
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.

    Can i use if else in comparing the total votes for each candidate?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    yes, you can, and you should.

  15. #15
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    yes, you can, and you should.
    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("")
    }

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  17. #17
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    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
    }

  18. #18
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.
    I really do not know what to do maybe for having a less experience in using java and i cannot follow your instructions cause i'm getting confuse.......

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  20. #20
    pirate is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

Page 1 of 2 12 LastLast

Posting Permissions

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