• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Read and write only 1 char

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic