Results 1 to 8 of 8
  1. #1
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default error when testing program (for loop with nested if statement))

    Hi I'm trying to write a program that will request a number of candidates names and number of votes they've got and at the end output the candidate with the highest number of votes,when I've run this program it is giving me the correct output if the first candidate or the second candidate has the highest number of votes but not if the third (ie last candidate,no matter how many candidates are entered) has,would appreciate any help with this it's probably just something small


    Java Code:
    package electioncandidates;
    
    import javax.swing.JOptionPane;
    
    
    public class ElectionCandidates {
    
       
        public static void main(String[] args) {
    
            String strInput,
                   topCand = null,strInput2,strTemp;
    
            int vote=0,topVote=0,temp=0;
            
            strInput = JOptionPane.showInputDialog("What is candidates name");
            strInput2=JOptionPane.showInputDialog("What is candidates mark?");
            vote=Integer.parseInt(strInput2);
    
    
    
            for(int i=1;i<3;i++){
    
                
                    
    
            if(vote>topVote)
            {
                temp = vote;
                vote=topVote;
                topVote=temp;
                strTemp=strInput;
                strInput=topCand;
                topCand=strTemp;}
             
                    strInput = JOptionPane.showInputDialog("What is candidates name");
            strInput2=JOptionPane.showInputDialog("What is candidates mark?");
            vote=Integer.parseInt(strInput2);}
    
                
            
    
    
            JOptionPane.showMessageDialog(null,"The top candidate is "+topCand+" with "+topVote+"votes");
    
    
    
            
    
    
    
        }
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by leoshiner View Post
    [code] if(vote>topVote)
    {
    temp = vote;
    vote=topVote;
    topVote=temp;
    strTemp=strInput;
    strInput=topCand;
    topCand=strTemp;}
    Why all that juggling? If you want the variable 'topVote' to keep track of the highest vote so far and you also want to keep track of the topCand(idate), simply do this:

    Java Code:
    if (vote > topVote) {
       topVote= vote;
       topCand= strInput;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Why all that juggling? If you want the variable 'topVote' to keep track of the highest vote so far and you also want to keep track of the topCand(idate), simply do this:

    Java Code:
    if (vote > topVote) {
       topVote= vote;
       topCand= strInput;
    }
    kind regards,

    Jos

    thanks that's after cleaning up that part of the code but I am still having the same problem whereby if the last candidate entered has the maximum votes it outputs the next highest candidate,is there a way around this or will I have to use another kind of loop altogether

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by leoshiner View Post
    thanks that's after cleaning up that part of the code but I am still having the same problem whereby if the last candidate entered has the maximum votes it outputs the next highest candidate,is there a way around this or will I have to use another kind of loop altogether
    Nope, a single loop with a single if statement should be fine. Show us the code you cleaned up please.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Nope, a single loop with a single if statement should be fine. Show us the code you cleaned up please.

    kind regards,

    Jos
    here it is
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package electioncandidates;
    
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author Leon
     */
    public class ElectionCandidates {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            String strInput,
                    topCand = null, strInput2, strTemp;
    
            int vote = 0, topVote = 0, temp = 0;
    
            strInput = JOptionPane.showInputDialog("What is candidates name");
            strInput2 = JOptionPane.showInputDialog("What is candidates mark?");
            vote = Integer.parseInt(strInput2);
    
    
    
    
    
            for (int i = 1; i < 3; i++) {
    
    
    
    
    
                if (vote > topVote) {
    
                    {
                        topVote = vote;
                        topCand = strInput;
                    }
    
                    strInput = JOptionPane.showInputDialog("What is candidates name");
                    strInput2 = JOptionPane.showInputDialog("What is candidates mark?");
                    vote = Integer.parseInt(strInput2);
                }
            }
    
    
    
    
    
    
            JOptionPane.showMessageDialog(null, "The top candidate is " + topCand + " with " + topVote + " votes");
    
    
    
            
    
    
    
        
        }
        }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Your logic is still convoluted (and incorrect); have a look at this pseudo code:

    Java Code:
    int vote, topVote= -1;
    String cand, topCand= "";
    while more candidates to read {
       read cand and vote;
       if (vote > topVote) {
          topVote= vote;
          topCand= cand;
       }
    }
    print topCand and topVote;
    Note that variable topVote was set to -1 before the loop just to make sure that it is lower than any vote read.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The problem with the last vote is that it gets input at the end of the loop and so it never gets processed because the loop finishes. As JosAH shows in pseudocode, you need to input the vote at the top of the loop so it will always get processed.

  8. #8
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

Similar Threads

  1. Nested loop problem
    By jim01 in forum New To Java
    Replies: 0
    Last Post: 04-17-2011, 03:38 AM
  2. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 03:39 AM
  3. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 02:49 AM
  4. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •