Java election, arrays keep tally of votes?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 5870 Views - Last Post: 29 June 2011 - 01:27 PM Rate Topic: -----

#1 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Java election, arrays keep tally of votes?

Posted 28 June 2011 - 12:21 PM

The point of this program is that there are ten people, marked 0 -9 and it should count the votes for each. input is a list of numbers from 0-9 ended with a flag, so a value of one entered means person 1 gets a vote. The array should keep track of the votes.

This is a pretty rough start on my code, and my output is producing 0 for every single count.


public class VoteCount
{
    public static void main(String[] args)
    {
     Scanner input = new Scanner(System.in);
     final int FLAG = -999;
     int votes; 
     System.out.print("Enter votes for candidates 0-9. End with " + FLAG);
     votes = input.nextInt();
     int [] number;
     number = new int [votes];
     int [] candidate;
     candidate = new int [10];
     for (int i= 2; i < 10; i ++)
     {
      candidate [i] = 0;
      }
     System.out.println ("Candidate Number, Number of Votes");
     for (int i = 0; i  < 10; i++)
     System.out.println( i + "        " + (candidate[i]));
     System.out.println();


    }
}

This post has been edited by lolitacharm: 28 June 2011 - 12:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java election, arrays keep tally of votes?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 12:25 PM

Why do you need the two arrays? Also, int[] x = new int[10]; creates an int[] with 10 elements all initialized to 0. No need to loop through and assign all the elements values of 0.

Your problem is that you aren't assigning any of the inputs to the array. From the sounds of things, you should have 10 inputs, each of which is stored in an element of the array.
Was This Post Helpful? 0
  • +
  • -

#3 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 12:41 PM

by the way, I forgot to add that I don't want to use .parse
The input can be virtually unlimited (a list of numbers from 0-9. e.g. 8 6 5 7 5 3 0 9 4 3 5 7 3 2 5 5 -999
Do I even need a for loop? I just don't understand how assign each vote to the array[0], array [1], etc...
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 12:42 PM

Use a counter variable and a for loop. Assign elem[i] the value read in from the Scanner.
Was This Post Helpful? 0
  • +
  • -

#5 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 12:58 PM

I'm completely stuck on this one
Any advice or code to get me started?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 12:59 PM

Hint- use a for loop. This for loop will iterate through the entire array. The i variable is the index of the element in the array. Now just assign a single piece of input (scan.nextInt()) to the element in the loop.
for(int i = 0; i < arrayName.length; i++){

}


Was This Post Helpful? 0
  • +
  • -

#7 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 01:04 PM

Am I even heading in the right direction? :/

{
    public static void main(String[] args)
    {
     Scanner input = new Scanner(System.in);
     final int FLAG = -999;
     int votes; 
     System.out.print("Enter votes for candidates 0-9. End with " + FLAG);
     votes = input.nextInt();
     int [] candidate;
     candidate = new int [10];
     for (int i= 0; i < candidate.length; i ++)
     {
      candidate [i] = votes;
      }
     System.out.println ("Candidate Number, Number of Votes");
     for (int i = 0; i  < 10; i++)
     System.out.println(     i + "                        " + (candidate[i]));
     System.out.println();


    }
}

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 01:05 PM

You're on the right track. One thing- votes has a static value. It is the first number you read in. Don't you want to keep reading in the next inputs specified? If so, you want to assign scan.nextInt() to the elements, not votes.
Was This Post Helpful? 0
  • +
  • -

#9 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 01:10 PM

What do you mean by elements?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 01:11 PM

Arrays have elements. In int[] x = new int[]{3, 1, 5};, 3, 1, and 5 are all the values stored in the array elements.
Was This Post Helpful? 0
  • +
  • -

#11 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 01:19 PM

the part I'm getting stuck on is
candidate [i] = votes;


how can the input that is just a list of random number from 0-9 be put into a single variable and assigned to each candidate[]?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 01:21 PM

Have you read the Scanner documentation? The nextInt() method returns a single int. So votes is a static variable because you aren't changing its value.
Was This Post Helpful? 0
  • +
  • -

#13 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 01:31 PM

okay this is what I've come up with now. This is clearly not working

   public static void main(String[] args)
    {
     Scanner input = new Scanner(System.in);
     final int FLAG = -999;
     int votes; 
     System.out.print("Enter votes for candidates 0-9. End with " + FLAG);
     
     int [] candidate;
     candidate = new int [10];
     
     while (input.nextInt() != -999)
     {
     for (int i= 0; i < candidate.length; i ++)
     {
      candidate [i] = input.nextInt();
      }
    }
     System.out.println ("Candidate Number, Number of Votes");
     for (int i = 0; i  < 10; i++)
     System.out.println(     i + "                        " + (candidate[i]));
     System.out.println();


    }
}

Was This Post Helpful? 0
  • +
  • -

#14 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 02:09 PM

The point of this program is that there are ten people, marked 0 -9 and it should count the votes for each. Input is a list of numbers from 0-9 ended with a flag, so a value of 1 entered means person 1 gets a vote. The array should keep track of the votes. I'm kind of confused about how to incorporate the flag, and MAJORLY confused about how to assign input ( such as 3) to count as a vote for candidate [3]. Any thought??

Scanner input = new Scanner(System.in);
     final int FLAG = -999;
     int votes; 
     System.out.print("Enter votes for candidates 0-9. End with " + FLAG);
     
     int [] candidate;
     candidate = new int [10];
     
     
     {
     for (int i= 0; i < candidate.length; i ++)
     {
      candidate [i] = input.nextInt();
      }
      
     System.out.println ("Candidate Number, Number of Votes");
     for (int i = 0; i  < 10; i++) 
     System.out.println(     i + "                        " + (candidate[i]));
     System.out.println();


    }
}

Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2725
  • View blog
  • Posts: 11,457
  • Joined: 20-September 08

Re: Java election, arrays keep tally of votes?

Posted 28 June 2011 - 02:19 PM

You need a two-dimensional array


int[][] votes = new int[NUM_CANDIDATES][NUM_VOTES_PER_CANDIDATE];

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2