user input

I'm fairly new to Java(6 months) and I'm trying to mimic an old

DOS C program but in an applet.

I have been successful at written some Java graphics type

programs like the "bouncing ball".

Now all I want to do is print "Enter a string" on the applet

and then wait for the user to type a string followed by

the enter key.

In C this would look like:

printf("Enter a string");

gets(input);

I know that the print statement would be something like:

g2D.drawstring("Enter a string");

But how do I have a cursor appear on the applet, wait

for keystrokes and store them in an array?

Any suggestions?

[708 byte] By [jerryvtss] at [2007-9-27 14:27:23]
# 1
Use a JTextField add an ActionListener and procss the input when an event is emitted.Thomas
thomas.behr at 2007-7-5 22:18:21 > top of Java-index,Archived Forums,Java Programming...
# 2

If you're interested in reading a line at a time, as I think you are, wrap stdin in a BufferedReader: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

and then use the readLine method: String s = in.readLine();

*******************************************************************************

Answer provided by Friends of the Water Cooler. Please inform forum admin via the

'Discuss the JDC Web Site' forum that off-topic threads should be supported.

YATArchivist at 2007-7-5 22:18:21 > top of Java-index,Archived Forums,Java Programming...
# 3
Sorry, that was for an applet. Doh.
YATArchivist at 2007-7-5 22:18:21 > top of Java-index,Archived Forums,Java Programming...
# 4

To avoid having to use the JavaPlugin (if you use Swing classes in your applet), you could use the old AWT java.awt.TextField object. Add an action listener to your TextField and it will pick up the enter key being pressed. You can the use the getText() method on the TextField to read what the input is.

cgarethc at 2007-7-5 22:18:21 > top of Java-index,Archived Forums,Java Programming...