[java] application question

Started by
9 comments, last by pythonfan 21 years, 2 months ago
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?
Advertisement
System.in is the input stream from the console, read that.
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]
Petr Stedry
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?
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
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
Petr Stedry
linux sucks
Linux is the choice OS if you want rock solid stability while you edit that ascii text file.
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]
Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,fasting, good works and so on. Up until Doom, no one seemed to have thought about thedouble-barrel shotgun. Eat leaden death, demon... -- Terry Pratchett
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.

This topic is closed to new replies.

Advertisement