Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

voting program not working if theres a tie

this is my array voting program. it works as i wanted but now i need to add new feature to it.
"If there is a tie, then the program should print the names and votes of the candidates who share the highest votes."

how should i approach this? my thinking was that i need to find duplicate integers from array and then process them somehow. any ideas you beautiful people, i’m stuck with this part 🙁

import java.util.Scanner;

public class ArraysVoting {

    public static void main(String[] args) {
        int number, aanet1, isoin;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of candidates:");
        number = scanner.nextInt();
        System.out.print("Enter candidate's name:");
        String nimi = scanner.next();
        System.out.print("Enter " + nimi + "'s votes:");
        aanet1 = scanner.nextInt();
        int[] votes = new int[number];
        String[] ehdokkaat = new String[number];
        for (int i = 0; i < votes.length - 1; i++) {

            System.out.print("Enter candidate's name:");
            String x = scanner.next();
            ehdokkaat[i] = x;
            System.out.print("Enter " + ehdokkaat[i] + "'s votes:");
            int c = scanner.nextInt();
            votes[i] = c;

        }
        isoin = votes[0];
        int kohta = 0;
        for (int i = 1; i < votes.length; i++) {
            if (isoin < votes[i]) {
                isoin = votes[i];
                kohta = votes[i];
            }
        }
        System.out.println();
        System.out.println(ehdokkaat[kohta] + " is the winner with " + isoin + " votes!");
    }

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your program threw exceptions and did not work as expected for me I when I ran it. Here are my modifications

    int number, aanet1, isoin;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the number of candidates:");
    number = scanner.nextInt();

    int[] votes = new int[number];
    String[] ehdokkaat = new String[number];
    for (int i = 0; i < number; i++) {

        System.out.print("Enter candidate's name:");
        String x = scanner.next();
        ehdokkaat[i] = x;
        System.out.print("Enter " + ehdokkaat[i] + "'s votes:");
        int c = scanner.nextInt();
        votes[i] = c;

    }
    isoin = votes[0];
    int kohta = 0;
    for (int i = 1; i < votes.length; i++) {
        if (isoin < votes[i]) {
            isoin = votes[i];
            kohta = i;
        }
    }
    System.out.println();
    System.out.println(ehdokkaat[kohta] + " is the winner with " + isoin + " votes!");
}
  • Removed the duplicate logic before the for loop
  • In the kohta you do not save the index but the votes of the winner
  • The for loop was up to votes.length and although it is correct it is a bit confusing

Now for your question
There are multiple ways to go about it.
The simplest is to iterate through the votes array and find the max value.

Then you iterate again and you save the indexes of the array that have the value .

And then you use those indexes in the ehdokkaat array to find the winners.

You can see if you have a tie, by the amount of items in the array, list or whatever you use. So if it is one you have a winner otherwise there is a tie.

UPDATE:
I wrote it for you, did not test very extensively, I used arrays I hope you are ok with it.

    int number, aanet1, isoin;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the number of candidates:");
    number = scanner.nextInt();
    Integer[] votes = new Integer[number];
    String[] ehdokkaat = new String[number];
    for (int i = 0; i < number; i++) {

        System.out.print("Enter candidate's name:");
        String x = scanner.next();
        ehdokkaat[i] = x;
        System.out.print("Enter " + ehdokkaat[i] + "'s votes:");
        int c = scanner.nextInt();
        votes[i] = c;

    }
    int max = Collections.max(Arrays.asList(votes));
    List<Integer> winners = new ArrayList();
    for (int i = 0; i < votes.length; i++) {
        if (max == votes[i]) {
            winners.add(i);
        }
    }

    if (winners.size() > 1) {
        System.out.println();
        System.out.println(String.format("Tie (with %d votes) between:", max));
        for (Integer index : winners) {
            System.out.println(ehdokkaat[index]);
        }
    } else {
        System.out.println();
        System.out.println(ehdokkaat[winners.get(0)] + " is the winner with " + max + " votes!");
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading