jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes How to make a method exit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to make a method exit" Watch "How to make a method exit" New topic

How to make a method exit

Tony Carolla

Joined: Dec 05, 2005
Posts: 13
I have created the sample MIDI app in the headstart book, page 342. I am not sure if I should copy the source here, for copyright reasons and whatnot, but I have triple-checked to see that the code I entered is identical to what's in the book.

Basically, a method is invoked that does the following:

1) create a Sequencer
2) create a Sequence
3) create a Track in the Sequence
4) Add two events to the track
5) associate the sequence to the sequencer
6) invoke the start() method on the Sequencer.

The note plays, and I can change the pitch, duration, etc. But from the command-line, control never returns. I have to eventually press Ctrl-C... I added a System.exit(1); after the start() invocation, but then the command line returns before the sound plays.

Now, maybe I am getting ahead of myself (GUIs are in the next chapter), but after the note plays, the sequence is done, why won't the program exit?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Well, Sequencer has a stop() method - have you tried calling that? Of course you can't call it immediately or it will stop before you've played anything. Perhaps a call to Thread.sleep(10000) (ten seconds) followed by a call to stop()? When you get to the GUI section you'll probably cover how to call stop() in response to a user command instead. Until then, maybe using control-C is your best bet.

I don't know what this "headstart" book is - perhaps you mean Head First Java? If so perhaps this question is better answered by someone who has a copy of the book, and thus a better perspective on what the code is doing here.

"I'm not back." - Bill Harding, Twister
Tony Carolla

Joined: Dec 05, 2005
Posts: 13
hee hee... I meant Head First... not Head Start...

That did it. Actually, waiting ten seconds and invoking the stop method did not return the command line, but waiting ten seconds then issuing a System.exit(1) returns control.

I peeked ahead a bit in the book, and it looks like this will be covered later. Part of my problem is that I come from the procedural side of the house. I have been programming in procedural languages for years. I am still struggling to write in 'object-oriented' mode, but I will get there.

I agree. Here's the link: http://aspose.com/file-tools
subject: How to make a method exit
It's not a secret anymore!