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

    Default getting incorrect output nested for loop

    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");
    
    
    
            
    
    
    
        }
    
    }
    Last edited by leoshiner; 05-02-2011 at 10:28 PM. Reason: update

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use code tags.
    [code]
    YOUR CODE HERE
    [/code]

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

    Default

    Quote Originally Posted by sunde887 View Post
    Please use code tags.
    [code]
    YOUR CODE HERE
    [/code]
    just updated it there with code tags,I will do this in future

Similar Threads

  1. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 03:39 AM
  2. How to Separate the nested loop
    By venk123 in forum New To Java
    Replies: 3
    Last Post: 02-24-2011, 07:31 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. Class problem: getting incorrect output
    By mgm2010 in forum New To Java
    Replies: 9
    Last Post: 04-30-2009, 07:17 AM
  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
  •