简体   繁体   中英

Print values of arrayList in Java

The three errors that I am getting now are from the line:

printResults(election);

How would I fix that so it will print the total value of the votes? Thanks.

TesstCandidate4

public class TestCandidate4
{     
 public static void printVotes(List<Candidate> election) 
{
    for(int i = 0; i < election.size(); i++)
       System.out.println(election.get(i));
}

 public static int getTotal(List<Candidate> election)
{
  int total = 0;
  for(Candidate candidate : election )
  {
    total += candidate.numVotes;
  }
   return total;
}

 public static void printResults(Candidate[] election)
{
  double percent;
  System.out.println("Candidate        Votes Received      % of Total Votes");

 for (Candidate candidate : election)
{
  percent = (double) (candidate.votes()) / getTotal(election) * 100;
  System.out.printf("%-15s %10d %20.0f", candidate.getName(), candidate.votes(), percent);
  System.out.println();
}

}

  public static void replaceName(List<Candidate> election, 
                                String find, String replace) 
 {
   for(int index = 0; index < election.size(); index++)
      if (election.get(index).getName().equals(find))
           election.get(index).setName(replace);
 }

  public static void replaceVotes(List<Candidate> election, 
                                String find, int replace) 
 {
   for(int index = 0; index < election.size(); index++)
      if (election.get(index).getName().equals(find))
           election.get(index).setVotes(replace);
 }

    public static void replaceCandidate(List<Candidate> election, 
                                String find, String replace, int replaceV) 
 {
   for(int index = 0; index < election.size(); index++)
      if (election.get(index).getName().equals(find))
   {
     election.get(index).setName(replace);
           election.get(index).setVotes(replaceV); 
   }
 }


 public static void main(String[] args)
 {
    List<Candidate> election = new ArrayList<Candidate>();

    // create election
    election.add(new Candidate("John Smith", 5000));
    election.add(new Candidate("Mary Miller", 4000));        
    election.add(new Candidate("Michael Duffy", 6000));
    election.add(new Candidate("Tim Robinson", 2500));
    election.add(new Candidate("Joe Ashtony", 1800));  
    election.add(new Candidate("Mickey Jones", 3000));
    election.add(new Candidate("Rebecca Morgan", 2000));
    election.add(new Candidate("Kathleen Turner", 8000));
    election.add(new Candidate("Tory Parker", 500));
    election.add(new Candidate("Ashton Davis", 10000));

    System.out.println("Original results:");
    System.out.println();
    System.out.println(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );
    System.out.println();

    replaceName(election, "Michael Duffy", "John Elmos");
    System.out.println("Changing Michael Duffy to John Elmos:");
    System.out.println();
    System.out.println(election);
    System.out.println("Total of votes in election: " + getTotal(election) );   
    System.out.println();

    replaceVotes(election, "Mickey Jones", 2500);
    System.out.println("Changing Mickey Jones to 2500:");
    System.out.println();
    printResults(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );   
    System.out.println();

    replaceCandidate(election, "Kathleen Turner", "John Kennedy", 8500);
    System.out.println("Changing Mickey Jones to 2500");
    System.out.println();
    printResults(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );
    System.out.println();

   }

}

Candidate

public class Candidate
{
 // instance variables 
  int numVotes;
  String name;

/**
  * Constructor for objects of class InventoryItem
  */
  public Candidate(String n, int v)
  {
   // initialise instance variables
   name = n;
   numVotes = v;
  }
  public int votes() 
  {
    return numVotes;
  }
  public void setVotes(int num)
  {
    numVotes = num;
  }
  public String getName()
  {
    return name;
  }
  public void setName(String n)
  {
    name = n;
  }    
  public String toString()
  {
    return name + " received " + numVotes + " votes.";
 }

}

You are using an array, not an ArrayList. They are different.

You can learn about ArrayLists, and how to use them, here: http://www.homeandlearn.co.uk/java/array_lists.html

To create an ArrayList of candidates you'd use the following syntax:

ArrayList<Candidate> array_name = new ArrayList<Candidate>()

To pass an ArrayList as an argument you'd do something similar by:

public void method_name(ArrayList<Candidate> argument_name) { ... }

For clear explanation on arrays and arraylist, please read the following post: How to add new elements to an array?

I think you need also to rewrite your printResults in the following way (or similar):

public static void printResults(Candidate[] election)
 {
   double percent;
   int getTotalVotes = getTotal(election); //To calculate this once, it saves you execution time, with large numbers

   System.out.println("Candidate        Votes Received      % of Total Votes");

   for (Candidate candidate : election)
   {
      percent = (double) (candidate.votes()) / getTotalVotes * 100;
      System.out.printf("%-15s %10d %20.0f", candidate.getName(), candidate.votes(), percent);
      System.out.println();
   }
 }

Since election is an array, to get the x-th element, use election[x] . If it was a List , you would use election.get(x) .

So the line you asked to correct should be:

percent = (double) (election[x].votes()) / getTotal(election) * 100;

And the line after it:

System.out.printf("%-15s %10d %20.0f", election[x].getName(), election[x].votes(), percent);

But your code will still not work, because in the main method you add candidates in a List , but all the methods expect an array instead of a List . A quick fix would be to change the beginning of your main method like this:

List<Candidate> electionList = new ArrayList<Candidate>();

// create election
electionList.add(new Candidate("John Smith", 5000));
electionList.add(new Candidate("Mary Miller", 4000));
electionList.add(new Candidate("Michael Duffy", 6000));
electionList.add(new Candidate("Tim Robinson", 2500));
electionList.add(new Candidate("Joe Ashtony", 1800));
electionList.add(new Candidate("Mickey Jones", 3000));
electionList.add(new Candidate("Rebecca Morgan", 2000));
electionList.add(new Candidate("Kathleen Turner", 8000));
electionList.add(new Candidate("Tory Parker", 500));
electionList.add(new Candidate("Ashton Davis", 10000));

Candidate[] election = electionList.toArray(new Candidate[electionList.size()]);

That is, populating a List , but then converting it to an array.

A better solution will be to use a List everywhere. For example change this method:

 public static void replaceVotes(Candidate[] election, String find, int replace) { for (int index = 0; index < election.length; index++) if (election[index].getName().equals(find)) election[index].setVotes(replace); } 

Rewrite like this:

public static void replaceVotes(List<Candidate> election, String find, int replace) {
    for (Candidate candidate : election) {
        if (candidate.getName().equals(find)) {
            candidate.setVotes(replace);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM