12 Replies Latest reply: Apr 4, 2010 1:47 PM by 3004 RSS

    I have a small issue using parallel arrays

    843789
      Ok i need to make a program using 3 arrays. The program has to do with entering 5 candidates names and entering how many votes for each. The third array is one that holds the percentages of votes each candidate has. Ok, now my question is what am i doing wrong? The whole program works but when it outputs the percentage array all it comes up with is 0% for all five candidates.

      What am i doing wrong?

      heres the code:
      package voting;
      import java.io.*;
      import java.util.*;
      
      public class Voting {
      
           //initiating the arrays for the whole class
           static String [] nameList = new String[5];
           static int [] votes = new int[5];
           static int [] percentage = new int[5];
           
           public static void main(String[] args) {
                //initiating the scanner console
                Scanner console = new Scanner(System.in);
                
                //taking the input of the date as a string 
                System.out.println("Please enter the date: ");
                String date = console.next();
                
                //taking the candidates names and votes for each name
                for(int loop=0; loop < nameList.length; loop++)
                     {System.out.println("Last name of the candidate: ");
                     nameList[loop] = console.next();
                     
                     System.out.println("Number of votes: ");
                     votes[loop] = console.nextInt();
                     }
                
                System.out.println("These votes were taken " +date);
                
                //loop to output all of the candidates names and their corresponding votes
                for (int loop = 0; loop < votes.length; loop++)
                     System.out.print(nameList[loop]+ " recieved " +votes[loop]+ " votes.\n");
                
                //outputs the total number of votes found in method findSum()
                System.out.println("The total number of votes is: " + findSum(votes));
                
                //prints the percentages found in method findPercentage()
                for(int loop = 0; loop < votes.length; loop++)
                     System.out.print(nameList[loop] +" recieved "+percentage[loop]+ " % of the votes. \n");
                
                System.out.println("The winner is " +findWinner(votes) +".");
           }//end of main
           
           //method to find the sum of all the votes inputed
           public static int findSum(int[] votes){
                int sum = 0;
                
                //loop to add up all the votes and return the total votes
                for(int index = 0; index < votes.length; index++)
                     sum = sum + votes[index];
                return sum;
           }//end of findSum()
           
           public static void findPercentage(){
           
                //loop to find the percentages for each name and input them into the percentage array
                for(int loop = 0; loop < votes.length; loop++)
                     percentage[loop] = votes[1]/findSum(votes);
           }//end of findPercentage()
           
           public static String findWinner(int [] votes){
                int maxIndex = 0;
                String winner;
                
                for(int index=1; index < votes.length; index++)
                     if (votes[maxIndex] < votes[index])
                          maxIndex = index;
                
                return winner = nameList[maxIndex];
                
           }
      
      
      }//end of class
        • 1. Re: I have a small issue using parallel arrays
          795917
          Where is the invocation for findPercentage() method?
          • 2. Re: I have a small issue using parallel arrays
            843789
            oh wow... i didnt even add it haha. well i just put it in but it still comes up with the same thing. 0% for all the candidates.

            heres the new code
            package voting;
            import java.io.*;
            import java.util.*;
            
            public class Voting {
            
                 //initiating the arrays for the whole class
                 static String [] nameList = new String[5];
                 static int [] votes = new int[5];
                 static int [] percentage = new int[5];
                 
                 public static void main(String[] args) {
                      //initiating the scanner console
                      Scanner console = new Scanner(System.in);
                      
                      //taking the input of the date as a string 
                      System.out.println("Please enter the date: ");
                      String date = console.next();
                      
                      //taking the candidates names and votes for each name
                      for(int loop=0; loop < nameList.length; loop++)
                           {System.out.println("Last name of the candidate: ");
                           nameList[loop] = console.next();
                           
                           System.out.println("Number of votes: ");
                           votes[loop] = console.nextInt();
                           }
                      
                      System.out.println("These votes were taken " +date);
                      
                      //loop to output all of the candidates names and their corresponding votes
                      for (int loop = 0; loop < votes.length; loop++)
                           System.out.print(nameList[loop]+ " recieved " +votes[loop]+ " votes.\n");
                      
                      //outputs the total number of votes found in method findSum()
                      System.out.println("The total number of votes is: " + findSum(votes));
                      
                      //prints the percentages found in method findPercentage()
                      findPercentage();
                      for(int loop = 0; loop < votes.length; loop++)
                           System.out.print(nameList[loop] +" recieved "+percentage[loop]+ " % of the votes. \n");
                      
                      System.out.println("The winner is " +findWinner(votes) +".");
                 }//end of main
                 
                 //method to find the sum of all the votes inputed
                 public static int findSum(int[] votes){
                      int sum = 0;
                      
                      //loop to add up all the votes and return the total votes
                      for(int index = 0; index < votes.length; index++)
                           sum = sum + votes[index];
                      return sum;
                 }//end of findSum()
                 
                 public static void findPercentage(){
                 
                      //loop to find the percentages for each name and input them into the percentage array
                      for(int loop = 0; loop < votes.length; loop++)
                           percentage[loop] = votes[loop]/findSum(votes);
                 }//end of findPercentage()
                 
                 public static String findWinner(int [] votes){
                      int maxIndex = 0;
                      String winner;
                      
                      for(int index=1; index < votes.length; index++)
                           if (votes[maxIndex] < votes[index])
                                maxIndex = index;
                      
                      return winner = nameList[maxIndex];
                      
                 }
            
            
            }//end of class
            • 3. Re: I have a small issue using parallel arrays
              843789
              comes up with the same thing. 0% for all the candidates.
              percentage[loop] = (votes[loop]*100)/findSum(votes);
              Warning: what if sum of votes=0?

              Thanks!
              • 4. Re: I have a small issue using parallel arrays
                843789
                i know but when i test it i use: a,b,c,d, and e for the candidates names and 1,2,3,4, and 5 for the votes for each.

                earlier in the assignment my sum of votes comes up 15 but it still says 0% for the percentages. i will add in a line a accomodate if the sum of the votes is 0
                • 5. Re: I have a small issue using parallel arrays
                  843789
                  ok just tried it. now the percentages dont even print... WTF..

                  new code
                  package voting;
                  import java.io.*;
                  import java.util.*;
                  
                  public class Voting {
                  
                       //initiating the arrays for the whole class
                       static String [] nameList = new String[5];
                       static int [] votes = new int[5];
                       static int [] percentage = new int[5];
                       
                       public static void main(String[] args) {
                            //initiating the scanner console
                            Scanner console = new Scanner(System.in);
                            
                            //taking the input of the date as a string 
                            System.out.println("Please enter the date: ");
                            String date = console.next();
                            
                            //taking the candidates names and votes for each name
                            for(int loop=0; loop < nameList.length; loop++)
                                 {System.out.println("Last name of the candidate: ");
                                 nameList[loop] = console.next();
                                 
                                 System.out.println("Number of votes: ");
                                 votes[loop] = console.nextInt();
                                 }
                            
                            System.out.println("These votes were taken " +date);
                            
                            //loop to output all of the candidates names and their corresponding votes
                            for (int loop = 0; loop < votes.length; loop++)
                                 System.out.print(nameList[loop]+ " recieved " +votes[loop]+ " votes.\n");
                            
                            //outputs the total number of votes found in method findSum()
                            System.out.println("The total number of votes is: " + findSum(votes));
                            
                            //prints the percentages found in method findPercentage()
                            findPercentage(votes);
                            for(int loop = 0; loop < votes.length; loop++)
                                 System.out.println(nameList[loop] +" recieved "+percentage[loop]+ " % of the votes. \n");
                            
                            System.out.println("The winner is " +findWinner(votes) +".");
                       }//end of main
                       
                       //method to find the sum of all the votes inputed
                       public static int findSum(int[] votes){
                            int sum = 0;
                            
                            //loop to add up all the votes and return the total votes
                            for(int index = 0; index < votes.length; index++)
                                 sum = sum + votes[index];
                            return sum;
                       }//end of findSum()
                       
                       public static void findPercentage(int[] votes){
                       
                            //loop to find the percentages for each name and input them into the percentage array
                            
                            if (findSum(votes) !=0)
                                 for(int loop = 0; loop < votes.length; loop++)
                                      percentage[loop] = votes[loop]/findSum(votes);
                                 else
                                      System.err.println("No votes entered. Please start again."); System.exit(0);
                                 
                                      
                            
                                                
                                      
                       }//end of findPercentage()
                       
                       public static String findWinner(int [] votes){
                            int maxIndex = 0;
                            String winner;
                            
                            for(int index=1; index < votes.length; index++)
                                 if (votes[maxIndex] < votes[index])
                                      maxIndex = index;
                            
                            return winner = nameList[maxIndex];
                            
                       }
                  
                  
                  }//end of class
                  • 6. Re: I have a small issue using parallel arrays
                    795917
                    It's because the data type for percentage should not be int.
                    • 7. Re: I have a small issue using parallel arrays
                      843789
                      i have tried changing the data type between int, double, and object and still comes up zero's..

                      can you at least give me some sort of hint. i just need a jumpstart on this issue
                      • 8. Re: I have a small issue using parallel arrays
                        843789
                        can you at least give me some sort of hint. i just need a jumpstart on this issue
                        I gave you the solution in my first reply but you didn't notice it, the solution is you have to multiply by 100 to get the %age which you are not doing, here it is again:
                        percentage[loop] = (votes[loop]*100)/findSum(votes);
                        This is different from what you are doing, look closely...

                        and I warned that you would get divide by zero error if sum=0, you would have to overcome this problem using some conditional statement(s). That's what I said in earlier reply, though it was very short but I thought you would get it.

                        Thanks!
                        • 9. Re: I have a small issue using parallel arrays
                          843789
                          ANSWERED!!!!!!!!!!!!! thanks bud!!
                          • 10. Re: I have a small issue using parallel arrays
                            795917
                            Yes, TBM is right.

                            @TBM: Hi TBM. How are you? You must be preparing for your final semester exam, right?. All the best.
                            What about your campus recruitment? I am sure you must have already been placed with the best.
                            All the best for career. :-)

                            Regards, Swapnaja
                            • 11. Re: I have a small issue using parallel arrays
                              843789
                              @TBM: Hi TBM. How are you?
                              I am fine, Thanks! How do you know me? I am surprised, to say the least.
                              You must be preparing for your final semester exam, right?. All the best.
                              That's right... but I don't really prepare hard for exams ;-)
                              What about your campus recruitment? I am sure you must have already been placed with the best.
                              I got placed about 5-6 months ago, but fortunately or unfortunately ;-), I also got 12th rank in GATE, and now, I think it would be better for me if I do masters before going for job. So, I have applied for M.E.(CS) course at IISc, lets see what happens.. :)
                              All the best for career. :-)
                              Thanks again.
                              • 12. Re: I have a small issue using parallel arrays
                                3004
                                JigsnRigs wrote:
                                Ok, now my question is what am i doing wrong?
                                What you're doing wrong is that you're using parallel arrays.