Redux
Redux

Reputation: 25

Array (list?) required, but int found error

I have the following code:

import java.util.ArrayList;
public class TestCandidate2
{
public static void main(String[] args) {
 ArrayList<Candidate> election = new ArrayList<Candidate>();

 Candidate john = new Candidate("John Smith", 5000);
 election.add(john);
 Candidate mary = new Candidate("Mary Miller", 4000);
 election.add(mary);
 Candidate michael = new Candidate("Micheal Duffy", 6000);
 election.add(michael);
 Candidate tim = new Candidate("Tim Robinson", 2500);
 election.add(tim);
 Candidate joe = new Candidate("Joe Ashtony", 1800);
 election.add(joe);

 System.out.println("Results Per Candidate:");
 System.out.println("________________________");
 System.out.print("\n");
 int totalVotes = 0;
 int total = 0;

 for(Candidate dec : election) {
    System.out.println(dec.toString());
    total += dec.getVotes();
    totalVotes += dec.getVotes();
 }

 System.out.print("\n");
 System.out.println("Total number of votes in election: " + totalVotes);
}

public static void printVotes(ArrayList<Candidate> table) {
    for(int i = 0; i < table.size(); i++) {
       System.out.println(table.size()[i]);
    }
}

/**public static int getTotal(ArrayList<Candidate> table)  {
    int total = 0;
    for(int i = 0; i < table.size(); i++) {
       total = table[i].getVotes() + total;
    }
    return total;
}*/

/**public static void printResults(ArrayList<Candidate> table)  {
    double total = getTotal(table);
    System.out.print("Candidate               Votes Received              % of Total Votes");
    System.out.print("\n");
    for(int i = 0; i < table.length; i++) {
       System.out.printf("%s %17d %25.0f", table[i].getName(), table[i].getVotes(), ((table[i].getVotes() / total) * 100));
       System.out.println("");
    }
}*/
}

Now first off the error I think I should be getting is 'arraylist required, but int found', but instead the error that I am getting is 'array required, but int found'.

I do not know how I should fix the int i, for whenever I put [] to declare it as an array I still get errors. I have tried researching, but no one seems to have my exact problem.

Upvotes: 1

Views: 453

Answers (3)

dave
dave

Reputation: 11975

You appear to be accessing your Lists incorrectly. Instead of:

table[i].getVotes()

try:

table.get(i).getVotes();

Or avoid using indexes altogether. For example, you could rewrite getTotal() as:

public static int getTotal(List<Candidate> candidates) {
    int total = 0;
    foreach (Candidate c : candidates) {
        total += c.getVotes();
    }
    return total;
} 

The line producing the error:

System.out.println(table.size()[i]);

is a little confusing, but I think you want:

System.out.println(table.get(i).getVotes());

Upvotes: 2

Nir Levy
Nir Levy

Reputation: 12953

you should use table.get(i) instead of table[i]. this is an ArrayList not an array

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201487

You could use List.get(int) to get an element by position (not [] that's for arrays). Also, I suggest you program to the list interface. Like

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

You could also use a for-each loop and += like,

public static int getTotal(List<Candidate> table)  {
    int total = 0;
    for(Candidate c : table) {
       total += c.getVotes();
    }
    return total;
}

Upvotes: 1

Related Questions