Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2008, 06:54 PM
Member
 
Join Date: Apr 2008
Location: Banglore,India
Posts: 4
Sreejesh25 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-02-2008, 06:01 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
This may help you. I do something different here.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-02-2008, 07:19 AM
Member
 
Join Date: Apr 2008
Location: Banglore,India
Posts: 4
Sreejesh25 is on a distinguished road
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.....

Code:
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-02-2008, 07:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-02-2008, 07:58 AM
Member
 
Join Date: Apr 2008
Location: Banglore,India
Posts: 4
Sreejesh25 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-02-2008, 08:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-02-2008, 08:12 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by Sreejesh25 View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-07-2008, 06:48 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-08-2008, 04:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting each character from keyboard Sreejesh25 Advanced Java 1 04-02-2008 06:31 PM
Polled keyboard input through swing Prometheus Advanced Java 2 02-04-2008 05:05 PM
Assign a keyboard key to a JButton. gszauer AWT / Swing 1 12-15-2007 11:42 PM
reading text character by character bugger New To Java 2 11-09-2007 09:54 PM
Help with keyboard events? Bibendum New To Java 2 11-02-2007 03:51 AM


All times are GMT +3. The time now is 06:36 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org