• 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

program

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic