By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,481 Members | 3,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,481 IT Pros & Developers. It's quick & easy.

"do while" loop

P: 1
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the tutorials but when I change it from here I then get compiling issues... Can someone explain what I've done wrong?
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2. public class LloydPage_Assignment5 {
  3.   public static void main (String [] args) {
  4.  
  5. //  Enter Y to vote yes
  6. //  Enter N to vote no
  7. //  Enter Q to quit voting
  8.     int quitVote = 0;
  9.     int yesVote = 0;
  10.     int noVote = 0;
  11.  
  12. //  charAt looks at number in parenthesis, returns char at that location
  13. //    within string (the first character is at location 0)
  14. //    firstChar is going to be 'Y' or 'N' or 'Q' etc.
  15. //  Essentially it is a way to make a string into a char so that you may use string function
  16.  
  17.     String reply1 = JOptionPane.showInputDialog
  18.       (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
  19.     char userVote = reply1.charAt(0);
  20.  
  21. //  If the user enters Y or y then add 1 to the number of yes votes    
  22.  
  23.      do { 
  24.       yesVote = yesVote + 1;
  25.        reply1 = JOptionPane.showInputDialog
  26.        (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
  27.     userVote = reply1.charAt(0);
  28.     } while ((userVote == 'Y') || (userVote == 'y'));  
  29.  
  30. //  If the user enters N or n then add 1 to the number of no votes    
  31.  
  32.     do {
  33.       noVote = noVote + 1;
  34.        reply1 = JOptionPane.showInputDialog
  35.        (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
  36.     userVote = reply1.charAt(0);
  37.     } while ((userVote == 'N') || (userVote == 'n'));
  38.  
  39. //  If the user enters any variable other than Y||y, N||n, Q||q then ignore vote
  40.  
  41.     do {
  42.        reply1 = JOptionPane.showInputDialog
  43.        (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
  44.     userVote = reply1.charAt(0);
  45.     } while ((userVote != 'Y') || (userVote != 'y') || (userVote != 'N') || (userVote != 'n') || (userVote != 'Q') || (userVote != 'q'));    
  46.  
  47. //  If the user enters Q or q then
  48. //  use a showConfirmDialog box
  49. //  to ask if they really want to quit
  50.  
  51.     do {
  52.       quitVote = JOptionPane.showConfirmDialog
  53.       (null, "Are you sure you wish to Quit?");
  54.     } while ((userVote == 'Q') || (userVote == 'q')); 
  55.  
  56. //  Otherwise the loop ends and the totals must be shown in a
  57. //  showMessageDialog box
  58.  
  59.     do {  
  60.      JOptionPane.showMessageDialog(null, "The total is number of Yes votes is " + yesVote + "\n The total is number of No votes is " + noVote);
  61.     } while (quitVote == JOptionPane.YES_OPTION);
  62.  
  63.   }
  64. }
Sep 23 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
Your logic is incorrect; you want to collect Y or N votes until the user wants to
quit; then you want an additional confirmation from the user. If the user really
wants to quit you display the totals and quit. Otherwise you just continue.
Check your text book for the switch() statement and don't be afraid to add a
few simple methods for sub-tasks. Here's a first idea:

Expand|Select|Wrap|Line Numbers
  1. for (char vote= 'A'; !quitVote(vote); ) {
  2.    vote= getVote();
  3.    switch (vote) {
  4.       case 'y': case 'Y': yesVote++; break;
  5.       case 'n': case 'N': noVote++; break;
  6.       case 'q': case 'Q': quitVote++; break;
  7.       default: illegalVote(vote);
  8.    }
  9. }
  10.  
The getVote() and illegalVote() methods handle what they have to handle.
The quitVote() method checks whether or not the user really wants to quit.

kind regards,

Jos
Sep 23 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.