Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java Example Not Compiling  RSS feed

 
Leo Gonzalez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm very new to java and recently purchased the "Head First Java" book which was authored by Kathy Sierra (Yowza!) and Bert Bates. I'm reading through the first chapter and running through the looping examples. I typed out the 99 bottles of beer on the wall to see what it would do, but can't get it to compile. I know the example says there is a flow, but the program should work, what gives? Talk about being confused!! I'm really confused! Below is my code, what am I doing wrong? I tryed to compile in Eclipse, a simple text editor called ConTEXT, and through DOS. Thanks!
Errors:
> Executing: C:\Program Files\ConTEXT\ConExec.exe "C:\Program Files\Java\jdk1.5.0_07\bin\javac.exe" "BeerSong.java"

BeerSong.java:31: ')' expected
System.out.println("No more bottles of beer on the wall");
^
BeerSong.java:30: not a statement
(
^
BeerSong.java:38: 'class' or 'interface' expected
} // end class
^
BeerSong.java:46: 'class' or 'interface' expected
^
4 errors
> Execution finished.



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 > 0)
{
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
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leo, welcome to the JavaRanch!



You need to include a class declaration(correct terminology?) above the main method like:

And look in the code above in my post, some characters are used for giving arguments, and some are used to surround blocks of code (I'm sorry, I can't remember the technical term).

Keep posting, and see if you can see what's wrong.
[ December 12, 2006: Message edited by: Nathan Leniz ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above excerpt you accidentally used a "(" instead of a "{". This is the cause of at least two of those errors and probably all of them.
 
vinay varanasi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes a parenthesis was used accidentally instad of a brace the code is fine. do change that.

you Should use the match brace function of your editor to avoid this kinds of problems in future.
 
Leo Gonzalez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syntax error! "(" should have been "{". DANG~! Thanks for the help, I should have noticed that. Just starting out , but I want to learn this!!!
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first umpteen million classes I typed to get a feel for the language gave me such a wide range of errors it wasn't even funny. I also have mistyped a "(" instead of a "{".

Welcome to the addiction of Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!