Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

"Head First Java" book help  RSS feed

 
Rob Hermann
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am as new as new can get. I've successfully worked through several on-line tutorials and am picking java up rather quickly, so I feel even more naive for asking:

If anyone has worked through the O'Reilly Head First Java book by Sierra and Bates maybe you could help.

For the 99 bottles of beer song on page 14
I am typing in the code exactly as it appears on the page and yet it will not compile.
Is it not supposed to at this point or am I missing something?

Thanks,
Rob
 
rajib chakrabarti
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could u post the code?
 
Gopi Krrishna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code as given the book is

public class BeerSong
{
public static void main(String[] args)
{
int beerNum=99;
String word= "bottles";
while(beerNum>0)
{
if(beerNum==1)
{
word="bottle";
}
System.out.println(beerNum+" "+word+" of the beer on the wall");
System.out.println(beerNum+" "+word+" of the 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 the beer on the wall");
}
else
{
System.out.println("no more bottles of beer on the wall");
}
}//end of while loop
}
}


if u type this as given and compile it, it works perfectly. If its not compiling for u then u would have made some error while typing. please check it out else post the error.
 
Rob Hermann
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've refined my trouble and questions in a new post. Thank you for your comments here.

Rob
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about this program. I am new as well and worked on the very same one. This has the same output error I got in that it starts at 59 yet the logic of the program would indicate it WILL start at 99. Is it the limitation of the console window perhaps using javac?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
No, I copied and pasted what was printed and it printed from 99 to 1.

Now, there is an error in that the coding oughtn't to be all in the main method, but not the error you were finding.
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I ask what you used to run it in?
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I have to say it is either something you have typed in wrong or maybe an issue with the way you are compiling it. I'm working through this book now (which I love by the way) and had no problems running it.

As a side note, there are some really fun projects toward the latter section of the book.

Mark
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RJ,

are you SURE it's starting at 59? could it be that your console only allows about 300 lines in its history?

in other words, the program outputs a LOT of lines of text, and i'm betting what you're missing has simply scrolled off the top.

i'm assuming you're on some Windows flavor. i'm running Win2k, so it may be a little different if your on another version... but, try right-clicking on the titlebar of your console, and click properties. on the Layout tab, check the 'screen buffer size', and set it to something like 1000, then run the program again.
[ August 07, 2006: Message edited by: fred rosenberger ]
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My GOD do I feel stupid. Thank you, yes, that was it. I guess i have to remain humble. I thought for SURE there was a coding way to make it work. Im using this book as well. Is it considered good? http://www.amazon.com/gp/product/0596009208/sr=1-1/qid=1154964464/ref=pd_bbs_1/104-3201231-8459938?ie=UTF8&s=books
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
books are like music... everyone has their own personal taste and learning style.

Having said that, many folks around here swear by that book. the fact that it was written by one of the founders of the javaranch, and the fact that both authors frequent these parts is a bonus. if you check out the SCJP forum, you'll soon see Bert answering questions.

so, yes, i'd say it's a great book to learn java. it is NOT a reference book, but it doesn't sound like you need that yet.
 
Michael McCollum
Greenhorn
Posts: 3
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code worked for me.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess i have to remain humble.
Humility is a virtue, but don't feel bad because a bit of confusing design in terms of numbers of lines displayed has caught you out.
 
Mark Scheper
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There

I just copied and pasted the code.
javac BeerSong.java does compile. So now for the program to run:

java BeerSong

i got this error: Exception in thread "main" java.lang.NoClassDefFoundError: BeerSong

I am new to this and haven't a clue what this is about... Anyone?

Thanks, Mark
 
Mark Scheper
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, ok. So now i first try the "search" and then ask a question (if any).

I had to set the ClassPath. Now it works
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!