aspose file tools*
The moose likes Beginning Java and the fly likes Help with java do while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with java do while loop" Watch "Help with java do while loop" New topic
Author

Help with java do while loop

Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
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,

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
And welcome to the Ranch
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
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

Joined: Feb 19, 2013
Posts: 17
Campbell Ritchie wrote:And welcome to the Ranch


Thanks Ritchie!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
Thanks guy, I've finally managed to execute the right code with a help from my professor. I really appreciate it.
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
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
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

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?
Mike Mannz
Greenhorn

Joined: Feb 22, 2013
Posts: 1
So what did you do to fix it?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
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

Joined: Feb 19, 2013
Posts: 17
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

Joined: Aug 07, 2007
Posts: 2366
    
  50
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

Joined: Feb 19, 2013
Posts: 17
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with java do while loop