I need to count the number of votes for candidates... I'm getting an unreachable statement error on "int[] array = new int[6];"

 Scanner sc= new Scanner(System.in);
    int a= sc.nextInt();
    return a;


   int[] array = new int[6];

   int mitt;
   mitt= array [a];

   int rick;
   rick= array [a];

   int newt;
   newt= array [a];

   int ron;
   ron= array [a];

   int other;
   other= array [a];

   int invalid;
   invalid= array [a];



   if (a==0)
   {mitt++;
   System.out.println(mitt);}
   else if (a==1)
   {rick++;
   System.out.println(rick);}  
   else if (a==2)
   {newt++;
   System.out.println(newt);}
   else if (a==3)
   {ron++;
   System.out.println(ron);}
   else if (a==4)
   {other++;
   System.out.println(other);}
   else
   {invalid--;
   System.out.println(invalid);}  


   return array [a];

Recommended Answers

All 17 Replies

How much code gets executed after a return statement?

this is the whole code...

import java.util.Scanner;

public class Election
{
public static void main(String[] args)
{
System.out.println("Hello. Let's collect votes.");
System.out.println("The voting begins on the count of 10.");

for (int x=1; x < 11; x++)
{
System.out.println(x);
}
//printBallot();
//getVote();
//System.out.println(getVote() );

while (getVote() != 5)
{
//getVote();
}

}

public static void printBallot()
{
System.out.println("Your choices: 0-Mitt, 1-Rick, 2-Newt, 3-Ron, 4-other, 5-To end the election now.");
}
public static int getVote()
{
printBallot();
Scanner sc= new Scanner(System.in);
int a= sc.nextInt();
return a;

int[] array = new int[6];

int mitt;
mitt= array [a];

int rick;
rick= array [a];

int newt;
newt= array [a];

int ron;
ron= array [a];

int other;
other= array [a];

int invalid;
invalid= array [a];

if (a==0)
{mitt++;
System.out.println(mitt);}
else if (a==1)
{rick++;
System.out.println(rick);}
else if (a==2)
{newt++;
System.out.println(newt);}
else if (a==3)
{ron++;
System.out.println(ron);}
else if (a==4)
{other++;
System.out.println(other);}
else
{invalid--;
System.out.println(invalid);}

return array [a];
}

}

Add an if statement before the return a; statement so it the compiler thinks there is a possibility that the code following the return could be executed.

Why do you have code following the return a; statement?

okay i just moved the if statement and array above the return a; ...i didnt realize that was there. i need to create an array and an if statement to count up all the votes each candidate gets and i cant figure that part out because i just compiled and ran it and it doesnt work.

it doesnt work.

What does it do? What is the output that shows you it is not working?
Can you explain what you want the code to do?

The whole program is a voting machine and a scanner built in so people can vote for who they want and once you type 5 the voting will stop and then after that it needs to count up the votes for each candidate the steps were...

1- Print Hello. Let's collect votes. Compile and TEST!
2- Add to V1: Print The voting begins on the count of 10. Then write a for loop to make the computer print 1, 2, ... up to 10, one number per line. Compile and test!!
3-Make a static void method named printBallot that prints: Your choices: 0­Mitt, 1­Rick, 2­Newt, 3­Ron, 4­other, 5­To end the election now. And make your main method call it so you can test it. Compile and test!
4-Make a static int method named getVote( ), and
(1) Code a call to getVote( ) in main for testing purposes. Make main print out the return value for testing purposes. How? Simply code System.out.println(getVote( ) );
(2) Make the getVote( ) method call printBallot()
(3) Make the method then instantiate a Scanner with new Scanner(System.in) Of course, also declare a Scanner reference variable like sc so you can use your Scanner. Remember to import java.util.Scanner; and make sure you have sc = what it should so sc refers to the new Scanner.
(4) Make the method input the vote with sc.nextInt( ) (5) Make the method return the number the voter typed as the return value.
Compile, debug, and TEST!!
5-After removing the call to getVote for testing purposes, add to V4 a loop in main( ) to make it repeatedly get votes and stop the the first time somebody votes 5 Compile and TEST!
6-Use an array or 4 separate variables for the 4 candidates, one more for "other", and a 6th for invalid votes; and code a compound if-else statement like: if( ... ) { ... } else if ( ... ) { ... } else if ( ... ) { ... } else if ( ... ) { ... } else if ( ... ) { ... } else { ... } Make the program count how many votes each of the candidates 0, 1, 2, 3 got, count how many votes of 4 (for "other"), and count how many invalid votes (negative, or > 5). When the voting is finished (somebody votes 5), how many of each kind of vote must be printed.

I did all those steps already except 6 and I cannot figure it out.

and the for part 6 it shows me its not working because after voting it just prints a -1 which isnt correct because i need the count of votes added for each candidate separately

You need to post the code that you are having problems with and the output from the code.
Add comments to show what the correct output should be.

Scanner sc= new Scanner(System.in);
int a= sc.nextInt();
return a;

int[] array = new int[6];

int mitt;
mitt= array [a];

int rick;
rick= array [a];

int newt;
newt= array [a];

int ron;
ron= array [a];

int other;
other= array [a];

int invalid;
invalid= array [a];

if (a==0)
{mitt++;
System.out.println(mitt);}
else if (a==1)
{rick++;
System.out.println(rick);}
else if (a==2)
{newt++;
System.out.println(newt);}
else if (a==3)
{ron++;
System.out.println(ron);}
else if (a==4)
{other++;
System.out.println(other);}
else
{invalid--;
System.out.println(invalid);}

the program printed -1... i need a count of votes for each candidate so i need an array and an if else statement that prints how many votes mitt, rick, newt, ron, other, and invalid got

Where does it print the -1? Is -1 the wrong value?

Try debugging the code by adding println statements to print out the value of a so you can see what the computer is testing with the if statements.

Please post a copy of the code that you are executing. What you posted is NOT valid code and should not compile or execute.

and still has the return a; in front of a lot of code.
can you post your current code?

import java.util.Scanner;

public class Election
{
  public static void main(String[] args)
  {
    System.out.println("Hello. Let's collect votes.");
    System.out.println("The voting begins on the count of 10.");

  for (int x=1; x < 11; x++)
  {
    System.out.println(x);
}
  //printBallot();
  //getVote();
  //System.out.println(getVote() );

   while (getVote() != 5)
  {
    //getVote();
   }

}

  public static void printBallot()
  {
    System.out.println("Your choices: 0-Mitt, 1-Rick, 2-Newt, 3-Ron, 4-other, 5-To end the election now.");
}
  public static int getVote()
  {
    printBallot();
    Scanner sc= new Scanner(System.in);
    int a= sc.nextInt();


   int[] array = new int[6];

   int mitt;
   mitt= array [0];

   int rick;
   rick= array [1];

   int newt;
   newt= array [2];

   int ron;
   ron= array [3];

   int other;
   other= array [4];

   int invalid;
   invalid= array [5];



   if (a==0)
   {mitt++;}
   else if (a==1)
   {rick++;}  
   else if (a==2)
   {newt++;}
   else if (a==3)
   {ron++;}
   else if (a==4)
   {other++;}
   else
   {invalid++;} 



   System.out.println("mitts votes" + mitt);
   System.out.println("ricks votes" + rick);
   System.out.println("newts votes" + newt);
   System.out.println("rons votes" + ron);
   System.out.println("other votes" + other);
   System.out.println("invalid votes" + invalid);

    return a;  

}

i need the last part to print me votes of each person that voted numbers 0-4, 5 stops the voting, and any negative or number greater than 5 needs to be counted in invalid votes... I cant get it to add the votes collectively at the end

When an int array is created all its values are 0.
Can you explain why you are assigning values from the array to variables? Like the following:

mitt= array [0];

Where do you use the value of a that was read in from the user?

Can you explain what you want the getVotes() method to do? It reads a number from the user and then what should it do?

i need to assign variables so i can use in withinn my if statements so i can collectively count uo votes... "int a= sc.nextInt();" a is part of the scanner...the gets votes methos uses a scanner so when you run the program people can vote 0-5, 5 ends the voting. this method also needs to count up the votes each person got at the end

The code assigns 0 to all the variables. There are no values in the array, it is full of zeros.

how could i change that to make it work?

To make code work you need to have design for what the code is supposed to do.
Can you make a list of the simple steps that you want the code to do? Here is a start:
Prompt user to enter some data
read the data from the user
???? What next ????

Now finish the list of the steps the code is supposed to do.

Explain what the int array with 6 elements is to be used for.
Explain what the variables: mitt, ron, rick, etc are to be used for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.