Two Laptop Bag
The moose likes Beginning Java and the fly likes Head First Java Ch 11 Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Ch 11 Example" Watch "Head First Java Ch 11 Example" New topic

Head First Java Ch 11 Example

Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
I tried typing and compiling the example in ch 11 of Head First Java, which is a program that plays a note. When I run it, the command window frezes up as if the program won't stop running. Then I have to shut down the command window, start it all over again, and I don't think the program is supposed to do that. Here is the code:

This is pretty cool stuff, and I wish I didn't have to restart the command window, re-type in the directory, etc jsut to run the program! Is there any way to get around this? The program itself is pretty much copied word for word from the book.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10828

Have you learnt while loops yet?
Using a while loop, you can ask the user if he wants more fun or should the application quit.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
I do know about while loops. I'm not sure how I would use one here though. The problem is that the command line window is freezing. If the command line window did not freeze, then I could put everything in a loop no problem. The program should play a note then terminate. Instead, it plays a note then freezes up. Even if I get rid of the player.start() line, it won't play a note, but it still freezes the command window, as if the program is not terminating somehow. Which leaves me to believe that it's not a problem with midi or anything like that, it's probabily just something in the code that I am not catching.
Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
Well, I have messed around some more, and the problem is really that the program won't terminate when it is done. Everything else works. Does anyone have any insight as to why the program won't terminate? Here is my code so far:
As you can see, it just gets input from the user which is 2 numbers, then it calls the method play which plays the note with the given instrument, and it just keeps doing that until the user enters "Q" at the right time.
Pete D Donoghue

Joined: Feb 13, 2013
Posts: 1

I hope this is still of some use to somebody?

try adding

to the end of the play method.

I'm looking at the same tutorial and was trying to figure out why i get no sound output. I get no compilation or runtime errors, but no sound is produced.
I'm guessing this might be machine specifc, but not sure where to start looking?
Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
Welcome to the Ranch.

try adding


to the end of the play method.

I've never used the sound stuff so I may be wrong but I would have thought it played sounds asynchronously in which case that would stop the player immediately, probably before it's even played any sound.
Calling player.close() once "Q" is pressed would be a good idea though.
I agree. Here's the link:
subject: Head First Java Ch 11 Example
jQuery in Action, 3rd edition