File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "program" Watch "program" New topic


Sam Gavis

Joined: Oct 27, 2007
Posts: 2
Could someone tell me what is wrong with this program?

class BeerSong{
public static void main (String[] args) {
int beerNum = 99;
String word = "bottles";

While(beerNum > 0); {

if (beerNum ==1) {
word = "bottle"; //singular, as in one bottle
System.out.println(beerNum + "" + word + "of beer on the wall");
System.out.println(beerNum + "" + word + "of beer");
System.out.println("Take one down.");
System.out.println("Pass it around");
beerNum = beerNum - 1;

if (beerNum > O) {
System.out.println(beerNum + "" +word + "of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}//end else
}//end while loop
}//end main method
}//end class
[ October 27, 2007: Message edited by: Sam G-H ]
marc weber

Joined: Aug 31, 2004
Posts: 11343

"Sam G-H,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name. Initials are okay for a first name, but not a last name.

You can edit your display name here. Thank you for your prompt attention!


Originally posted by Sam G-H:
Could someone tell me what is wrong with this program? ...

The compiler errors are telling you what's wrong...

But if you haven't seen these before, they might not be obvious.

First, on line 6, the compiler thinks that "While" is the name of a method you're trying to call. The reason is that Java is case sensitive, so "While" (with an uppercase 'W') is not the same as the keyword "while" (with a lowercase 'w').

Second, on line 17, the compiler thinks that you're trying to compare beerNum to some variable called 'O' because you've mistyped the letter 'O' instead of zero.

Once you've corrected these 2 errors, the code will compile, but then you will have a really curious problem when you try to run it. This code will enter an infinite loop and appear to "freeze up" without any output. (You will need to press Ctrl+C to break out.)

The reason is on line 6...

while(beerNum> 0); {

Do you see what's wrong here?
[ October 27, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: program
It's not a secret anymore!