Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TEXT INPUT

 
K. Joshua Chi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println happens to be the display or print statement of Java for outputting Strings etc. What is the method that enables the user to get data from the user like in forms, or in a program that requires a user to input data.
Thanks.
Joshua
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For "forms" you're probably talking about Swing or AWT GUI on a fat client, or maybe HTML forms on a web client. These are pretty involved topics. If you want to write programs that just run on your PC, try the Sun Swing Tutorial

For command line programs System.out does the writing, so it makes sense that System.what? will do the reading. You'll have to dig into buffered readers and such to read a line a time from the user. Take a shot at it and post what you come up with. The gang here will help you tweak it from there.

Enjoy exploring!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For forms (entered data, not optional data), usually a
javax.swing.JTextField(), or a javax.swing.JOptionPane.showInputDialog()

for command prompt, go back to the beginner's main page and scroll down to the subject "How do I..." where you'll find a detailed explanation/example
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Joshua Chi, you might like to take a look at the example and description I posted in the "How do I....?" thread.
 
Peter van der Linden
author
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the method that enables the user to get data from the user like in forms, or in a program that requires a user to input data.


Well, those are two different things. Michael already posted the answer for a text field in a GUI (like in Windows Forms).

For a command line program, you can read a line from stdin and parse it to extract a number
from it. If you are using Java 5, there's a simpler way. You can use the new class java.util.Scanner.
You wrap a scanner around anything that can be a suitable source of chars, such as the input stream System.in. Here's an example:


This is a lot simpler than before! There are nextFOO() methods for each primitive type and for
object types that make sense, like String. This isn't raw I/O - the input isn't watched char
by cahr, so the user has to type return before any of it is seen.

Anyway, if you are using Java 5, you will enjoy this.

Cheers,

Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic