• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Input from a keyboard

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! Please advice how can I read user's input from a keyboard and save it to a variable?
Thank you.
 
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where you want to read the input of the keyboard on Console ?
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through this Java™ Tutorials section. In many cases, the easiest way to do that is to use System.in and a Scanner object. The API documentation for Scanner gives a simple example.
Beware: with Scanner it is occasionally necessary to discard a line.
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that helped me.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a method I use:



Something like


would store console input in a String variable (until your user presses Enter).

The fine print: This method reads a line from standard input, which is not always (but usually) the same as "from console". It returns an empty String and not null for reasons I do not want to confess (hint: laziness is a powerful force). And using a BufferedReader in this method is a bit like using cannons to shoot down sparrows, but it was also a bit of a training example for me. The exception handling should have been more exact but it works for me...
 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That method is incomplete because you are not closing the Reader in a finally block.
 
Jan-Henrik Clausen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That method is incomplete because you are not closing the Reader in a finally block.



Ah, this encourages me to a question that - being a newbie - I haven't solved myself yet:

When I use this:



I get a compiler error, because "input.close()" throws an exception which must be handled. How can I guarantee the freedom of the used system resources? One way would be to add another try-catch-clause:



That would compile but if an error during closing happens there is no way to free the resources. Is there any method I overlooked which simply deallocates the resources bound and does nothing else? Or is "close()" *it* and an error could only occur during a major earthquake or large sun flares?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A nested try/catch is the appropriate answer, although I might refactor it out. If it's a long-running program why not just keep it open and not create a new reader every time you want to read something?
 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with David N that you might do well to keep the Reader open. This is how you can nest try blocksObviously you can do different things with the input from simply returning one line. You would also omit the catch (FileNotFoundException) block if reading from System.in, because System.in doesn't use any Files.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic