473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"do while" loop

1 New Member
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
1 4588
JosAH
11,448 Recognized Expert MVP
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

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

Similar topics

6
12213
by: Sybren Stuvel | last post by:
Hi there, Is it possible to use an assignment in a while-loop? I'd like to do something like "loop while there is still something to be read, and if there is, put it in this variable". I've been a C programmer since I was 14, so a construct like: while info = mydbcursor.fetchone(): print "Information: "+str(info)
11
2260
by: Wayne Folta | last post by:
Two observations about PEP-315: 1. It's clever, addresses a definite "wart", and is syntactically similar to try/except. But it's syntax seems like an acquired taste to me. 2. It is a very general construct, which might be what is called for. But I wonder if most of the time it would be used to accomplish something like:
6
71975
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
9
1640
by: morpheus | last post by:
Hi Group, When I run this: # include <stdio.h> int main(){ int c=0; while ( (c=getchar()) != EOF && c != ' ' && c != '\t' ) printf("foo"); if (c == '8') putchar(c);
1
1267
aprilmae36
by: aprilmae36 | last post by:
Nice day to all... Here I am again, having another problem with loops... lol... Can you please help me make a while loop program that the output will be like this: 1 121 12321 1234321 123454321 :-)
14
19412
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a strange workaround construct): -- do { // ...
3
2140
by: archeryguru2000 | last post by:
Hello, I've been stumped for several weeks on this particlar program. At work here, we have 30 machines that we record scrap data on. I take this data and create spreadsheets (boring) that can be used for analysis. Anyway, I was wondering how I could change this "for" statement to a much more condensed amount of code. for i in range(30): while hold == 1: hold_01.append( hold ) hDate_01.append( hold ) ...
2
3225
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate" modifier) and then give the visitor a link to read the whole article. But since "the_content" is a...
1
2336
by: Kerem Gümrükcü | last post by:
Hi All, i am too tired now to find this out by my own, but why does my while loop "while(readline==Console.ReadLine())" needs twice return hit to read the line, after setting "Console.TreatControlCAsInput = true". How can i make it work like it would be set to "false", so that i only must hit the return once to get the input,... TIA
11
1647
by: cokofreedom | last post by:
First off let me state that I really enjoy using Python. I am a 3rd year student and have been using python for 3 months, (thanks to trac!). I do not consider myself an experienced or clever programmer, but I am able to get by. Something I love about Python is that almost everything you do can be written in pseudo code then carried across into Python without a great- deal of difference. But reading through the warts and reading about...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.