Re: Percentage Array Help
Quote:
cannot find symbol
symbol: variable i
The compiler can not find a definiton for the variable: i that is in scope(within the same pair of{}s) where it is being used. Check that i is defined for where you are trying to use it.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Percentage Array Help
I guess what I am stuck on is actually how do I assign the votes for each candidate to the percentage variable at the end not every time a vote is inputed by the user so this is as far as can go without having a problem.
I can set up the double percentage now but cant print it outside the brackets only inside which isnt any use because the assignment of values hasnt completed so its not correct to have it there.
Code java:
import java.util.Scanner;
class Election
{
public static void main(String args[]) throws Exception
{
Scanner in = new Scanner(System.in);
String[] candidate = new String[5];
int[] votes = new int[5];
int totalVotes = 0;
int currentCandidate = 0;
int sum = 0;
while(currentCandidate < candidate.length)
{
System.out.println("Please enter candidates last name: ");
candidate[currentCandidate] = in.next();
currentCandidate++;
}
for(int i = 0; i < candidate.length; i++)
{
System.out.println("Please enter the total votes for " + candidate[i]);
votes[i] = in.nextInt();
sum += votes[i];
double percentage = (double) votes[i] / (double) sum * 100;
}
}
}
Re: Percentage Array Help
Quote:
cant print it outside the brackets
Define the variable outside of the brackets within the same pair of {}s where you want to print it.
Add values to the variable inside the loop.
What is the formula for computing a percentage? Shouldn't the division be done AFTER the all the values have been summed up?
Re: Percentage Array Help
Quote:
Originally Posted by
Norm
Define the variable outside of the brackets within the same pair of {}s where you want to print it.
Add values to the variable inside the loop.
What is the formula for computing a percentage? Shouldn't the division be done AFTER the all the values have been summed up?
im really confused if i move it out like so
Code java:
for(int i = 0; i < candidate.length; i++)
{
System.out.println("Please enter the total votes for " + candidate[i]);
votes[i] = in.nextInt();
sum += votes[i];
}
double percentage = (double) votes[i] / (double) sum * 100;
it just says
Error: cannot find symbol
symbol: variable i
location: class Election
i don't really get what you are asking me to do?
Im just focusing on the percentage part of the program after i have taken in the names and the votes just to test it works before adding in the end so just want to a print out of all the percentages for now to see they are running ok. this is my first week doing Arrays and Im still not used to them and I have been given no examples with this only getting highest numbers and averages etc so thats why im so lost.
Re: Percentage Array Help
What is the formula for computing a percentage? What values should your program use?
Your code is not following the formula.
Re: Percentage Array Help
I don't know any formula other than what i said the candidates vote / the total votes and * by 100 I haven't been given a formula.
Re: Percentage Array Help
What variable contains: the candidates vote
and what variable has: the total votes
Re: Percentage Array Help
vote is the candidates vote and sum is the total votes i changed it so its simpler
Code java:
import java.util.Scanner;
class Election2
{
public static void main(String args[]) throws Exception
{
Scanner in = new Scanner(System.in);
String[] candidate = new String[5]; // candidates last name
int[] votes = new int[5]; // candidates votes
int sum = 0; // total votes
for(int i = 0; i < candidate.length; i++)
{
System.out.println("Please enter candidates last name: ");
candidate[i] = in.next();
System.out.println("Please enter the total votes for " + candidate[i]);
votes[i] = in.nextInt();
sum += votes[i];
double percentage = (double) votes[i] / (double) sum * 100;
}
}
}
Re: Percentage Array Help
Can you use sum before you've added in all the values?
Re: Percentage Array Help
I'm looking for help with this I don't know what I am doing wrong I know that having
double percentage = (double) votes[i] / (double) sum * 100;
in the for loop isnt doing what I want it to do because its not finished looping but as I have said if I declare it in another array it just says that it cant find the variable. I'm only doing programming in college a couple of week and only had one lecture on parallel arrays so I don't know what to do that why I am asking you?
she also covered how to search an array and then get different outcomes should I be doing that to get the percentage?
I know how to get it if its a normal program its just arrays Im not fully aware of the structures.
Re: Percentage Array Help
Did you understand my comment about computing the value of sum BEFORE using it to compute the percentage?
Code :
double percentage = (double) votes[i] / (double) sum * 100;
AFTER computing the value of sum, then you can use it to compute the percentage for each candidate. The above statement needs to be in another loop where the data for each candidate is printed.
Re: Percentage Array Help
no i don't understand what you mean if you could just give me an example of what you are talking about then I would know.
If you mean that I cant work with the percentage until the loop has finished getting the value of sum that's what I am trying to do but don't know how
Re: Percentage Array Help
Quote:
cant work with the percentage until the loop has finished getting the value of sum
Yes that is correct.
AFTER computing the value of sum, then you can use sum to compute the percentage for each candidate in another loop.
percentage = candidates vote / the total votes * by 100
Re: Percentage Array Help
got it working thanks for the help!
Just one thing how to I format the percentage to 2 decimal places?
Code java:
import java.util.Scanner;
class Election2
{
public static void main(String args[]) throws Exception
{
Scanner in = new Scanner(System.in);
String[] candidate = new String[5]; // candidates last name
int[] votes = new int[5]; // candidates votes
double[] percentage = new double[5]; // candidates percentage
int sum = 0; // total votes
for(int i = 0; i < candidate.length; i++)
{
System.out.println("Please enter candidates last name: ");
candidate[i] = in.next();
System.out.println("Please enter the total votes for " + candidate[i]);
votes[i] = in.nextInt();
sum += votes[i];
}
for(int i = 0; i < votes.length; i++)
{
percentage[i] = (double) votes[i] / (double) sum * 100.0;
}
int max = votes[0];
for (int i = 0; i < votes.length; i++)
{
if(votes[i] > max)
max = votes[i];
}
System.out.println(" The result of the election is:" + "\n");
System.out.println("Candidate" + "\t" + "Votes" + "\t" + "% of Total Votes" + "\t");
for(int i = 0; i < votes.length; i++)
{
System.out.println(candidate[i] + "\t" + votes[i] + "\t" + "%" + percentage[i] + "\t");
}
System.out.println("\n" + "The winner of the election is: " + max);
}
}
Re: Percentage Array Help
Look at the DecimalFormat class.
Re: Percentage Array Help
just ran it again and I dont have the winners name just the votes how do I do that?
do I need to set up another array for the winner?
and DecimalFormat class means nothing to me lecturer just gave this example from a rainfall program:
String fs = String.format("Even month rainfall average: %.2f", evenMonthAverage);
S.O.P(fs)
but thats not same as my program so dont know how to fit that in to my result output or if I am even meant to put in in there.
Re: Percentage Array Help
Quote:
DecimalFormat class means nothing to me
You need to learn how to read the API doc for Java classes.
The API doc link: Java Platform SE 7
Find the link to the class in the lower left panel and click on it to get the API doc for the class in the main panel.
Quote:
I dont have the winners name
Where are the names?
Re: Percentage Array Help
Quote:
Originally Posted by
Norm
Where are the names?
the names are the candidate variable.
but need to output the name of the candidate with the max votes which I have but can only print out the max votes not the candidates name that had the max votes.
Re: Percentage Array Help
If candidate is an array then you can use it the same way you used the contents of the other arrays when printing.
This line seems to already use it:
Code :
System.out.println(candidate[i] + "\t" + votes[i] + "\t" + "%" + percentage[i] + "\t");
To print only the name associated with the the max value, you need to save the index into the array where the max value was found.
Re: Percentage Array Help
I don't understand how to do that how to save the index into the array where the max value was found?
I'm really no expert I find if I am given an example then I learn from my mistakes you may aswell be telling me to speak Chinese here.
Re: Percentage Array Help
Here are the steps you need to do:
Define an int variable that will hold the index of the max value
In the loop where the search for the max value is done:
every time a value is assigned to the max variable, save the value of the index in the int variable defined in the first step
At the end of the search, max has the biggest value and the int variable has the index where max was found.
Re: Percentage Array Help
im sorry but I don't know how to do what you are saying to do.
Quote:
save the value of the index in the int variable defined in the first step
Re: Percentage Array Help
Try writing a small testing program to work out the technique.
Define a testing class with a main() method.
Add these two lines to the main() method:
Code :
public class Testing {
public static void main(String[]args) {
int[] anArray = {1,4,3,7};
int maxIdx = 0;
// add loop here to find max value in anArray
}
}
then write a loop to find the max value in the array: anArray
When that works, we'll work on saving the index of where the max value was in: maxIdx.
Quote:
save the value of the index in the int variable defined in the first step
maxIdx = <the index for the current location of max>; // save the value of the index
Re: Percentage Array Help
I did it <:-P finally!!
Thank u so much for the help I can now submit it and go to bed!!! :)
Code java:
The result of the election is:
Candidate Votes % of Total Votes
a 1 6.67
b 2 13.33
c 3 20.00
d 4 26.67
e 5 33.33
The winner of the election is: e
--- Update ---
except in my program the table is tabbed the right way!!