javaoopvoting

How to fix "looping and candidate voting" error in Java


I'm making a voting system, and want the voting system to loop until the user enters 0 and the candidate that wins will be outputted

two classes

 package javaexamcode; 
   import java.util.Scanner;

   public class JavaExamCode {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Nominee mickey = new Nominee("Mickey Mouse");
        Nominee donald = new Nominee("Donald Duck");
        Nominee minnie = new Nominee("Minnie Mouse");
        Nominee goofy = new Nominee("Goofy");

        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println();
            System.out.println("The presidential nominees are:");
            System.out.println("1. Mickey Mouse");
            System.out.println("2. Donald Duck");
            System.out.println("3. Minnie Mouse");
            System.out.println("4. Goofy");
            System.out.print("What is your vote? ");

            try{

                int vote=in.nextInt();

                if (vote==0){
                    break;
                }else if (vote == 1){
                    mickey.increaseVote();
                }else if (vote == 2){
                    donald.increaseVote();
                }else if (vote == 3){
                    minnie.increaseVote();
                }else if (vote ==4){
                    goofy.increaseVote();
                }
                else {
                    System.out.println("invalid");
                }
                break;
            } catch (Exception e){
                System.out.println("Invalid entry, try again");
                continue;

            }

        }
        Nominee[] candidates = new Nominee[]{mickey,donald,minnie,goofy};
        Nominee president = winner(candidates);

        System.out.println("The winner is " + president.toString() + " with " + president.totalVotes() + " votes.");
    }

    public static Nominee winner(Nominee[] all){
        Nominee most = all[0];

        for (int i = 1; i < all.length - 1; i++){
            if (all[i].totalVotes() > most.totalVotes()){
                most = all[i];
            }else if (all[i].totalVotes() == most.totalVotes()){
                String newName = all[i].toString() + " " + most.toString();
                most = new Nominee(newName, (all[i].totalVotes() + most.totalVotes()));
            }
        }

        return most;
    }

}






package javaexamcode; 
public class Nominee{
    private String name;
    private int votes;

    public Nominee(String name){
        this.name = name;
    }

    public Nominee (String name, int votes){
        this.name = name;
        this.votes = votes;
    }

    public void increaseVote(){
        votes++;
    }

    public String toString(){
        return name;
    }

    public int totalVotes(){
        return votes; 
    }

}

There is also an error when you type 4 then it outputs that the winner is mickey and minnie with 0 votes. Help would be greatly appreciated

I have searched online but could not find much


Solution

  • In your method:

    public static Nominee winner(Nominee[] all){
        Nominee most = all[0];
    
        for (int i = 1; i < all.length - 1; i++){
            if (all[i].totalVotes() > most.totalVotes()){
                most = all[i];
            }else if (all[i].totalVotes() == most.totalVotes()){
                String newName = all[i].toString() + " " + most.toString();
                most = new Nominee(newName, (all[i].totalVotes() + most.totalVotes()));
            }
        }
    
        return most;
    }
    

    your for loop is ignoring the last candidate (#4 in this case) Instead of the looping condition i < all.length - 1 you need to loop to i < all.length instead.