Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2004, 01:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading one character and then continuing?

How do I read one character from System.in and then continue, without pressing the enter key!? In the following piece of code you need to press the enter key after some characters have been entered...
Code:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int c = br.read();
I would like to have the user choose a one-digit number, but without giving the user the possibility of entering a whole string. How do I do this!?

Thanks

Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old February 15th, 2004, 08:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I haven't been able to solve your problem but as an aside, are you aware that by using your code, if I entered the number 1 then int c would contain 49? In ascii 49 equates to 1.

Sorry if you had already thought this through, I thought it best to mention it in case you hadn't.

Cheers

Martyn
 
Old February 15th, 2004, 09:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yep, I knew that, but this is not by problem. I am using a switch to branch the outcome, and it works. The problem is to move one as soon as one key has been pressed. Thanks anyway.

;)

Jacob.
 
Old February 15th, 2004, 05:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There is no way to access keyboard input without the user typing ENTER or RETURN; and no way to have non-echoed keyboard input. This is a problem for command line password input (e.g. for server code).
(Review ID: 57901

See this url:

http://developer.java.sun.com/develo...s/4236007.html

Cheers

Martyn
 
Old February 15th, 2004, 05:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, thanks a lot! It was nice to get some closure on this topic, eventhough it didn't solve my problem! My solution has then become like this piece of code shows...
Code:
String s;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
    showDialog(); 
    s = br.readLine();
    switch(s.charAt(0))
    {
        case '1':
        etc. That is, the user can type whatever, but I only use the first character (if possible), and at the same time uses (flushes) all other characters entered.

Thanks again

Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
character entity into numeric character entity srkumar XSLT 1 November 22nd, 2007 04:53 AM
Continuing Deployment Questions plb BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 March 6th, 2007 05:18 PM
Verify completion of Stored Proc before continuing busher .NET Framework 1.x 6 December 15th, 2006 07:58 PM
Continuing DEADLOCK Problem Ron Howerton SQL Server ASP 1 September 21st, 2005 02:55 PM
Continuing a line of code in C++ kevin777 C++ Programming 5 January 25th, 2004 05:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.