This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes we ask for user input in c by writing & (ampersand) but how to achieve this in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "we ask for user input in c by writing & (ampersand) but how to achieve this in java ?" Watch "we ask for user input in c by writing & (ampersand) but how to achieve this in java ?" New topic
Author

we ask for user input in c by writing & (ampersand) but how to achieve this in java ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

we ask for user input in c by writing & (ampersand) but how to achieve this in java ?
i mean how to take input from user ?


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

Here is the sample code for getting user input in Java through command-line:



Thanks and Regards,
Nilesh Sahni | nsahni@infocepts.com | www.infocepts.com
Kammaganti Kamal
Greenhorn

Joined: Jul 12, 2011
Posts: 6
naved momin wrote:we ask for user input in c by writing & (ampersand) but how to achieve this in java ?
i mean how to take input from user ?


Read about "System.in" in java .If not , then make further reading on this .. this will solve your question
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
naved momin wrote:we ask for user input in c by writing & (ampersand) . . .
No you don't. The & supplies an address for the data to be put into, from the scanf() procedure in C.

It is possible to use Readers, as you have already been shown, but it is much easier to use instances of the java.util.Scanner class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
N Sahni wrote:Here is the sample code for getting user input in Java through command-line: . . .
But you didn't close the Readers.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

It's a bit easier in Java 6:

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Campbell Ritchie wrote:
N Sahni wrote:Here is the sample code for getting user input in Java through command-line: . . .
But you didn't close the Readers.

Which is a good thing in this case. Closing the Readers would close System.in which will cause it to become useless within the entire JVM until it exits.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Just saw that readLine() can take a prompt. So this is N Sahni's example in Java 6.
Of course, you could make it shorter by in-lining the expression for value, at a slight cost to readability.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Rob Spoor wrote:. . . Closing the Readers would close System.in which will cause it to become useless within the entire JVM until it exits.
I never knew that. Thank you.

Beware of Console; if you don't use the java tool to start your application, System.console() may return null, and you will suffer an Exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: we ask for user input in c by writing & (ampersand) but how to achieve this in java ?
 
Similar Threads
Swaping Primitive Data types in java
How to input something ?
Multiple inputs for Java Runtime
user input values
problem while reading from console