== How do I do this simple thing in JAVA? ("cin << " in C++ ) ==
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   joey_corleon
Posted On:   Saturday, October 26, 2002 02:55 AM

Hi,


I am currently making a java test tool.
My problem is that I don't know how to get the user's entries :o(

In C++ for instance I would use cin < < ..etc ...


How do I do in JAVA to retrieve the entry of the user????


In some cases I need to retreive a single letter corresponding to an option in a menu (e.g. an option in the menu e.x. 1, 2, 3 etc ...) - In other case I need to retrieve words or sentances ...


Thanks for your help!

Re: == How do I do this simple thing in JAVA? ("cin &lt;&lt; " in C++ ) ==

Posted By:   Joe_Cheng  
Posted On:   Sunday, October 27, 2002 05:25 AM

If you want to get fancy, you can use the static methods on JOptionPane to pop up dialog boxes. :)

For example:

String firstName = JOptionPane.showInputDialog("What is your first name?");

There are other methods on this class to create Yes/No prompts, drop-down option lists, etc.

Re: == How do I do this simple thing in JAVA? ("cin &lt;&lt; " in C++ ) ==

Posted By:   Jeanne_Boyarsky  
Posted On:   Saturday, October 26, 2002 02:36 PM

It's much easier in C++ than Java. First you have to open a reader. Then read in the appropriate data type.

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
char ch = in.read();
String s = in.readLine();

Note that all of this must be done inside a try/catch block for an IOException.


Other related classes:
  1. If you want a numeric data type, you have to parse it:
     Integer.parseInt(string) 

  2. If you want to parse words, you should use StringTokenizer

Fuller descriptions are in the API. Very useful once you know the name of the class to look under :)

Re: == How do I do this simple thing in JAVA? ("cin &lt;&lt; " in C++ ) ==

Posted By:   Eelco_Hillenius  
Posted On:   Saturday, October 26, 2002 06:14 AM

To begin with... learn how to use search engines!


See this for a simple example or this (TextReader.java) for an idea of a helper class.


Or just learn how to work with the Java io package.


Good luck,
Eelco
About | Sitemap | Contact