• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

program

 
Sam Gavis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!

-Marc

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic