| 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: 675
|
|
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: 32599
|
|
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: 32599
|
|
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: 19214
|
|
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
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: 32599
|
|
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.
|
 |
 |
|
|
subject: we ask for user input in c by writing & (ampersand) but how to achieve this in java ?
|
|
|