This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the sample code for getting user input in Java through command-line:

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a bit easier in Java 6:

 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80071
410
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic