[java] application question
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?
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]
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]
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?
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
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
quote:Original post by Anonymous Poster
linux sucks
Holy shit, your insight is enourmous.
Than you for your contribution to our evergrowing community.
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement