Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
N Sahni
Ranch Hand
Posts: 55
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the sample code for getting user input in Java through command-line:

 
Kammaganti Kamal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bit easier in Java 6:

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic