|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-01-2008, 06:54 PM
|
|
Member
|
|
Join Date: Apr 2008
Location: Banglore,India
Posts: 4
|
|
|
getting each character from keyboard
I want to read each character from the keyborad to do some action on each key.
But the System.in.read() was waiting for ENTER key to be pressed.
I want System.in.read() returning one character each time I'm pressing one character on the keyboard.
Please help...
The code is like this.
int ch;
while ('\n' != (ch = System.in.read())) {
System.out.println("You pressed " + ch
+ ".System waiting for next character.Press ENTER to exit");
}
System.in.read() should not wait for the user to press the ENTER key.
|
|

04-02-2008, 06:01 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
This may help you. I do something different here.
char getCha;
int tmp = 0;
InputStreamReader inReader = new InputStreamReader (System.in);
OutputStreamWriter outWriter = new OutputStreamWriter (System.out);
try{
while(tmp != -1){
tmp = inReader.read ();
getCha = (char) tmp;
outWriter.write(getCha);
}
}
catch(IOException e){
System.out.println (e.getMessage());
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-02-2008, 07:19 AM
|
|
Member
|
|
Join Date: Apr 2008
Location: Banglore,India
Posts: 4
|
|
|
Still problem exists
hi Eranga,
I added one System.out.println() in your code.Still the problem exists.
What I want is , whenever user pressed a key in the keyboard, some action should be triggered(Here i gave a print). But now only when user is pressing ENTER key all action are triggered sequentially. i don't want to use ENTER key.
Please check.....
char getCha;
int tmp = 0;
InputStreamReader inReader = new InputStreamReader (System.in);
OutputStreamWriter outWriter = new OutputStreamWriter (System.out);
try{
while(tmp != -1){
tmp = inReader.read ();
System.out.println("You pressed one key");
getCha = (char) tmp;
outWriter.write(getCha);
}
}
catch(IOException e){
System.out.println (e.getMessage());
}
Last edited by Eranga : 04-02-2008 at 07:53 AM.
Reason: CODE tags are included
|
|

04-02-2008, 07:42 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
Ok, as much as possible put your output here. Hurry up. I'll explain it to you, rather putting the code directly.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-02-2008, 07:58 AM
|
|
Member
|
|
Join Date: Apr 2008
Location: Banglore,India
Posts: 4
|
|
|
Output
This is the output I'm getting.
Here I typed "abcd" as input
********************
abcd
You pressed one key
You pressed one key
You pressed one key
You pressed one key
You pressed one key
You pressed one key
*********************
The output I'm expecting is ,
******************
a
You pressed one key
b
You pressed one key
c
You pressed one key
*******************
I don't want to wait for the ENTER key to trigger an action.
please help
|
|

04-02-2008, 08:09 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
Grate, see there you have press four keys in a row. And I think after that you have press the Enter key also, right? But the output you have entered here is wrong. Did you copied it from the console or just type it here?
You should get the five lines there. Not six as you listed here. Only one line additionally should be there. If you type five characters you get six lines, for 10 characters you get 11 lines, and so on.
So what is the addition line. It's the newline. Enter key of the keyboard also have a character, that is the newline. So in your key sequence, 'abcd' you have new line at the end.
So you have to avoid that newline character. Now change the while loop as follows.
tmp = inReader.read ();
getCha = (char) tmp;
if(getCha != '\n'){
System.out.println("You pressed one key " + getCha);
}
See what happened.
I think on your first post, you try to exit the process by pressing Enter key. Normally it is not proper way to do. I think you already see it, in may application ask to press the key Q or any other key for exit. Not the Enter key.
If you still want to use the Enter key for it, I think it is not easy. It can but not easy.
Hope this is help to you.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-02-2008, 08:12 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
Originally Posted by Sreejesh25
I don't want to wait for the ENTER key to trigger an action.
Hmm, I never try such thing. May be action listeners may help, but I'm confusing that how to avoid Enter key from there. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-07-2008, 06:48 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
|
This has been asked here before not long ago. As far as i'm aware, you cannot use System.in without pressing the ENTER key at the end. Its the only way..
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-08-2008, 04:39 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
Yep, as far as I know it's the only way to do...
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|