Results 1 to 8 of 8
- 05-03-2011, 12:23 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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"); } }
- 05-03-2011, 01:12 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-03-2011, 01:58 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
- 05-03-2011, 02:07 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 05-03-2011, 03:48 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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"); } }
- 05-03-2011, 04:07 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Your logic is still convoluted (and incorrect); have a look at this pseudo code:
Note that variable topVote was set to -1 before the loop just to make sure that it is lower than any vote read.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;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-03-2011, 04:33 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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.
- 05-03-2011, 06:41 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Nested loop problem
By jim01 in forum New To JavaReplies: 0Last Post: 04-17-2011, 03:38 AM -
Nested Loop
By sehudson in forum New To JavaReplies: 2Last Post: 03-11-2011, 03:39 AM -
can some one help me with nested loop?
By keycoffee in forum New To JavaReplies: 10Last Post: 01-25-2010, 02:49 AM -
nested for loop question
By javabob in forum New To JavaReplies: 3Last Post: 05-20-2008, 11:00 PM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks