Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Percentage Array Help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Percentage Array Help

    Im only starting arrays and im stuck on the percentage of the total votes. I have all the rest of it compiling Im just getting an error when I try with the percentage bit. I havent finished the last output as I know how to that just want to get the percentage working. If someone could please help would be greatly appreciated!!

    Error: cannot find symbol
    symbol: variable i
    location: class Election

    This is what I have been asked to do:

    Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, the votes received by that candidate and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.
    Hint: use parallel arrays

    A sample output is:

    Candidate Votes Received % of Total Votes
    Johnson 5000 25.91
    Miller 4000 20.72
    Duffy 6000 31.09
    Robinson 2500 12.95
    Murphy 1800 9.33

    Total 19300

    The winner of the election is Duffy



    This is my code:

    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 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];
      }
     
     
      for (int j = 0; j < votes.length; j++)
      {
        double percentage = (double) votes[i] / (double) sum * 100;
      }
     
      int max = votes[0];
      for (int k = 0; k < votes.length; k++)
      {
        if(votes[k] > max)
        max = votes[k];
      }
     
     
      System.out.println("  The result of the election is:" + "\n");
      System.out.println("  Candidate  " + "  Votes  " + "  % of Total Votes  " + "\n");
     
     
     }
    }
    Last edited by Gra89; December 7th, 2012 at 03:31 PM. Reason: java formatting


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    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;
     
       }
     
     
     
      }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Percentage Array Help

    Quote Originally Posted by Norm View Post
    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
     
     
     
     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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    What variable contains: the candidates vote
    and what variable has: the total votes
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Percentage Array Help

    vote is the candidates vote and sum is the total votes i changed it so its simpler

    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;
      }
     
     }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    Can you use sum before you've added in all the values?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    Did you understand my comment about computing the value of sum BEFORE using it to compute the percentage?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    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
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Percentage Array Help

    got it working thanks for the help!

    Just one thing how to I format the percentage to 2 decimal places?

     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);
     
     
     
     
     }
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    Look at the DecimalFormat class.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by Gra89; December 8th, 2012 at 07:42 PM. Reason: rainfall program details

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Percentage Array Help

    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.

    I dont have the winners name
    Where are the names?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Percentage Array Help

    Quote Originally Posted by Norm View Post

    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.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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:
     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.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Percentage Array Help

    im sorry but I don't know how to do what you are saying to do.

    save the value of the index in the int variable defined in the first step

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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:
    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.

    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
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    Gra89 (December 8th, 2012)

  26. #25
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Percentage Array Help

    I did it finally!!
    Thank u so much for the help I can now submit it and go to bed!!!

     
    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!!

Page 1 of 2 12 LastLast

Similar Threads

  1. Calculationg the percentage.
    By Purple01 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 25th, 2012, 08:25 AM
  2. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  3. How to get a System's total CPU usage percentage using a java program
    By userj2ee in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 12:28 AM
  4. It's not printing out the correct percentage...
    By JBow94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2010, 03:07 PM
  5. No Percentage?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 18th, 2009, 11:02 AM