|
Subject:
|
Reading one character and then continuing?
|
|
Posted By:
|
jacob
|
Post Date:
|
2/14/2004 12:14:05 PM
|
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...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.
|
|
Reply By:
|
Martyn
|
Reply Date:
|
2/15/2004 7:54:50 AM
|
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
|
|
Reply By:
|
jacob
|
Reply Date:
|
2/15/2004 8:01:15 AM
|
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.
|
|
Reply By:
|
Martyn
|
Reply Date:
|
2/15/2004 4:23:50 PM
|
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/developer/bugParade/bugs/4236007.html
Cheers
Martyn
|
|
Reply By:
|
jacob
|
Reply Date:
|
2/15/2004 4:40:37 PM
|
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...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.
|