File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read and write only 1 char

 
Paul Speijers
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like a pretty simple problem, but I've been looking my ass of for it and can't find it.

I want to read 1 character from stdin and disable the user to type more than 1 char, like the C getch() method. I've tried the read and readChar methods of many Readers and InputStreams classes (e.g. BufferedReader, InputStreamReader, DataInputStream, InputStream) but they all permit the user to type in a String and return the first character of the String when the user hits the enter button. I want to have the char returned immediately after the user hits a key and disable the user from typing more.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We'd all love to do this but Java doesn't directly support it. Would it be over your head to make your own console in a Swing GUI? I've never tried it but it sounds kinda fun if you have enough time.
 
Sumesh Kumar T N
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you could do some stuff with "KeyEvent" wherein you could watch the keyevent happen and then exit from the console....
i have not done it...but you could try it.....
Use a KeyListener and wait for a KeyEvent to occur....
Once it occurs, stop and do whatever you want....
iam sorry if the idea is stupid......
ALL THE BEST !!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic