• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with java do while loop

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my very first post on here so i apologize in advance if this is the wrong section for my question.

So i'm new to java and i've having some difficulties with java do while loop. I have a homework and i'm pretty much stuck. Below is the question i'm having issues with,

Use a do .. while loop to write the following in one dialog box. Initialize your count variable to 10 (this means assign it a value of 10) before you begin the loop. Notice that proper English grammar requires that we say one bottle (no "s" on bottle).
10 bottles of beer on the wall
9 bottles of beer on the wall
8 bottles of beer on the wall
7 bottles of beer on the wall
etc
etc
1 bottle of beer on the wall
0 bottles of beer on the wall

Here is what my code looks like:

import javax.swing.JOptionPane;

public class AssignmentFour
{
public static void main(String[] args)
{

String outputMessageSix = "";
int beerCount = 10;
int minusBeer = 0;
do
{

outputMessageSix = outputMessageSix + beerCount;
beerCount--;
outputMessageSix = outputMessageSix + " bottles of beer on the wall.\n";

}
while( beerCount >= 0 && minusBeer >= 0);
{

}//end while
JOptionPane.showMessageDialog(null, outputMessageSix);
}//end main
}//end class

My output looks exactly like this:

10 bottles of beer on the wall
9 bottles of beer on the wall
8 bottles of beer on the wall
7 bottles of beer on the wall
6 bottles of beer on the wall
5 bottles of beer on the wall
4 bottles of beer on the wall
3 bottles of beer on the wall
2 bottles of beer on the wall
1 bottles of beer on the wall
0 bottles of beer on the wall

So pretty much what I've been trying to do is display " 1 bottle of beer on the wall". However, i'm unable to do so and the output displays " 1 bottles of beer on the wall". How can i change "bottles" to "bottle" for that specific line only?

Any help is really appreciated.

Thanks,

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if the number is 1 you want to output "bottle", else you want to output "bottles"?

Does what I wrote there suggest any Java code?
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:So if the number is 1 you want to output "bottle", else you want to output "bottles"?

Does what I wrote there suggest any Java code?



Thanks for your response Clapham. Any idea how to write the code with using do while loop only? I'm new to all this and is going to take some time to get myself familiar with java.
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And welcome to the Ranch



Thanks Ritchie!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amir Ahmed wrote:Any idea how to write the code with using do while loop only?


Why don't you want to use Paul's suggestion? There is a statement that lets you do EXACTLY what you want. Someone suggests using it, and your response is that you want to do it using a statement that does NOT do what you want to do...

That's just odd.
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guy, I've finally managed to execute the right code with a help from my professor. I really appreciate it.
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Amir Ahmed wrote:Any idea how to write the code with using do while loop only?


Why don't you want to use Paul's suggestion? There is a statement that lets you do EXACTLY what you want. Someone suggests using it, and your response is that you want to do it using a statement that does NOT do what you want to do...

That's just odd.



Paul's suggestion would have been the best. However, its my professors requirement that we use only do while. Nothing else.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amir Ahmed wrote:its my professors requirement that we use only do while. Nothing else.


Seriously? "Nothing else" - so no println statements, no variable declarations...nothing but a do-while loop?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what did you do to fix it?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So what did you do to fix it?


Welcome to the Ranch.

Please don't tell me you have the same ridiculous requirement to solve this only using a do-while statement.
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

So what did you do to fix it?


Welcome to the Ranch.

Please don't tell me you have the same ridiculous requirement to solve this only using a do-while statement.



Sorry for the late response Tony, below is how i fixed it.

String message = "";
String fixed = "of beer on the wall.\n"; // fixed message that will
// display on output
int beerCount = 10;
String bottle = "";
do {
bottle = (beerCount == 1) ? "bottle" : "bottles"; // check beerCount and

messageSix += beerCount + " " + bottle + " " + fixed;
beerCount--;
}

while (beerCount >= 0);
{
JOptionPane.showMessageDialog(null, message);
}// end while
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry for the late response Tony, below is how i fixed it.


I'm glad to hear you solved it, but that isn't a solution using only while loops as you have used the tenary operator. Mind you, it is a good way to do it as, has already been said, restricting the solution to just while loops is ludicrous.

BTW make sure you format your code correctly, your 'while' statement appears to belong to the final set of curly braces when in fact it belongs to the set of curly braces immediately following the 'do' keyword. And please use code tags when posting code
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

Sorry for the late response Tony, below is how i fixed it.


I'm glad to hear you solved it, but that isn't a solution using only while loops as you have used the tenary operator. Mind you, it is a good way to do it as, has already been said, restricting the solution to just while loops is ludicrous.

BTW make sure you format your code correctly, your 'while' statement appears to belong to the final set of curly braces when in fact it belongs to the set of curly braces immediately following the 'do' keyword. And please use code tags when posting code



Thanks Tony. I had noticed they weren't in order and took care of them right away.
 
reply
    Bookmark Topic Watch Topic
  • New Topic