Archived

This topic is now archived and is closed to further replies.

pythonfan

[java] application question

Recommended Posts

I was thinking of doing a text adventure game in java(as an application); but I can''t for the life of me find, anywhere, information on reading text from a prompt. I just can''t find anything;I''ve checked all the books I have, and the java documentation. Might someone be able to enlighted me?

Share this post


Link to post
Share on other sites
Well,

IMHO writing a simple GUI application (one window with a text area and an input field) would do the trick. Unless, of course, your target machine is a text only terminal.

have fun

Petr Stedry

edit: replaced "linux console" with "text only"

[edited by - Petr Stedry on January 28, 2003 6:29:58 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Petr Stedry
Well,

IMHO writing a simple GUI application (one window with a text area and an input field) would do the trick. Unless, of course, your target machine is a linux console terminal.

have fun

Petr Stedry



Hey, what''s that supposed to mean? We get GUIs in Linux too you know As an aside, Windows has a console. So does Mac OS X. Why pick on Linux?

Share this post


Link to post
Share on other sites
Whoa ...
this was not intentional!

I myself am using linux to develop java games (gone the linux way since early 2002).

So there''s no need to flame me to the bone

The only possible reason, that I described a "text only terminal" as a "linux console terminal" is possibly my personal experience from times long gone.

I will edit my previous post to correct this, thanks for mentioning NuffSaid.




Petr Stedry

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
linux sucks


Holy shit, your insight is enourmous.

Than you for your contribution to our evergrowing community.

Share this post


Link to post
Share on other sites
I think what he's looking for is completely text-based.
In this case, instead of creating a basic GUI for reading input you can use a BufferedReader... follow carefully.

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

You see, the BufferedReader class "wraps around" a Reader, and InputStreamReader IS-A Reader. The InputStreamReader needs an input stream to read, and System.in is connect to standard input by default.

Then...
String input = stdin.readLine();

Of course, you'll want to read in other types, in which case you'll need the wrapper classes (Integer class, Double class, etc), particularly their parsing methods.

Example: Integer.parseInt(input);

But that's nonsense, you say, because the user doesn't have to enter a number! Well, in this case, if input is not a number a NumberFormatException will be thrown. =)

Hopefully I haven't made any really stupid errors.
EDIT: AHHH!!! I missed some important details!
1) You must import the java.io package (import java.io.*
2) You must be prepared to throw/catch IOException because it is a checked exception that may be thrown! I'm not sure as to the BEST way of handling this. Any comments?

This isn''t life in the fast lane, it''s life in the oncoming traffic.
-- Terry Pratchett

[edited by - Kentaro on January 28, 2003 5:02:57 PM]

Share this post


Link to post
Share on other sites
to quote Mel Brooks'' masterpiece, "The Pruducers":
"Dankeschon baby!"

Thanks a lot when I get my TAS to a playable stage, I''ll be sure to post for evaluation. Thanks again.

Share this post


Link to post
Share on other sites
I normally have all my methods that may generate exceptions in one try block.


    
//this is code that doesn''t handle the exception, but passes it

//to the calling method to handle.

//Good if the exception means that program execution can''t continue

//because something very screwed up has happened

try {
//call readLine, etc. Methods that throw IOException.

} catch(IOException e) {
throw e;
}



//This code just prints the stack trace (poor example of handling the exception).

//You handle the exception, and don''t rethrow it if you think that the exception is trivial and can be handled locally.

//you want program execution to continue.

try {
//call readLine, etc. Methods that throw IOException.

} catch(IOException e) {
e.printStackTrace();
}


That''s just my take. It probably isn''t best practice, but i can''t really see how else handle IOExceptions. IO is IMHO one of Java''s weakest points, thanks to its over use of checked exceptions.


Don''t know why it appeared on another thread..

Share this post


Link to post
Share on other sites