Prev: Newbie again. "Java Keyboard input" is a failure as a google search. What isn't?
Next: Your Helping Heart
From: Arne Vajhøj on 18 Jul 2010 22:46 On 18-07-2010 08:45, Stefan Ram wrote: > Andreas Leitgeb<avl(a)gamma.logic.tuwien.ac.at> writes: >> And the javadoc on java.lang.System's field "in" is another approach. > > Yes. But I wonder: Does anyone know a well-known Java > program (a program that is used by many people) that reads > what a user types with the keyboard from System.in? > > Even if someone would come up with such a program here, > I think they are very rare. They probbaly are. But so what? Such well known Java programs are usually not written by people that have to ask in cljp on how to do keyboard input. People that ask that are typical in the process of learning the Java language. And then console suddenly makes a lot more sense. > So why should I recommend something that I deem to be used > hardly ever in applied programming? Do you teach small children to use a tricycle or do you let them start by driving car because they will eventually be driving a car? Arne
From: Arne Vajhøj on 18 Jul 2010 22:48 On 18-07-2010 11:52, Simon Brooke wrote: > On Sun, 18 Jul 2010 10:59:06 +0000, Stefan Ram wrote: > >> Mike Barnard<m.barnard.trousers(a)thunderin.co.uk> writes: >>> So, can the Fount Of All Knowledge point me to a good tutorial on the >>> most efficient methods to get input from a user please? I don't expect >>> hand holding, honestly, just pointers to really useful tutorials. >> >> To get text from the keyboard, the most obvious means to me would be a >> javax.swing.JTextField. > > Errrrr.... WHY?!?!?!? > > It seems perverse to go to the overhead of building a complete WIMP user > interface to do > > for ( int c = System.in.read(); c> -1; c = System.in.read()) { > char ch = (char) c; > /* now do something with ch */ > } > > In practice something like the following would be more useful: > > string readLineFromStdin() { > StringBuffer buffy = new StringBuffer(); > bool reading = true; > > while ( reading) { > int c = System.in.read(); > > switch (c) { > case 10: > case 13: > case -1: > reading = false; > break; > default: > buffy.append( (char)c); > break; > } > } > return buffy.toString(); > } > > although in anything but the simplest utility programs you'd probably do > something a touch more sophisticated than that. No - you would do it a lot simpler than that in any program. BufferedReader or Scanner will save a lot of code. Arne
From: Tom Anderson on 19 Jul 2010 05:06 On Mon, 19 Jul 2010, Stefan Ram wrote: > Tom Anderson <twic(a)urchin.earth.li> writes: >> But if what you want to write right now is an interactive command-line >> program, then System.in is the only thing that helps you > > When one wants to read characters (like German Umlauts) with the console > encoding and this console encoding differs from the system encoding (as > sometimes under Windows), sometimes, its better to wrap > java.io.FileDescriptor.in with the required encoding than to use > System.in. Aha, i'd never thought of that. I am a little perplexed by this, though; given that System.in is a stream rather than a reader, i would have thought character encoding didn't enter into it. Could you elaborate on what goes wrong in the case you mentioned? > And then, suddenly, a Swing-program to do the same, might not be much > more difficult anymore. Perhaps. But it won't work so well over ssh :). tom -- Give the future a chance!
From: Martin Gregorie on 19 Jul 2010 07:21 On Mon, 19 Jul 2010 10:06:44 +0100, Tom Anderson wrote: > > Perhaps. But it won't work so well over ssh :). > Depends on where you're sitting: if that's in front of a Windows box using PuTTY then of course you're right, but if, like me, you're sitting in front of a laptop with an X-term server running and X11 forwarding enabled, then running a Swing GUI over ssh works just fine. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
From: Simon Brooke on 19 Jul 2010 07:33 On Mon, 19 Jul 2010 07:58:28 +0100, Mike Barnard wrote: > On Sun, 18 Jul 2010 22:48:28 -0400, Arne Vajhøj <arne(a)vajhoej.dk> wrote: > >>On 18-07-2010 11:52, Simon Brooke wrote: >>> On Sun, 18 Jul 2010 10:59:06 +0000, Stefan Ram wrote: >>> >>>> Mike Barnard<m.barnard.trousers(a)thunderin.co.uk> writes: >>>>> So, can the Fount Of All Knowledge point me to a good tutorial on >>>>> the most efficient methods to get input from a user please? I don't >>>>> expect hand holding, honestly, just pointers to really useful >>>>> tutorials. >>>> >>>> To get text from the keyboard, the most obvious means to me would >>>> be a javax.swing.JTextField. >>> >>> Errrrr.... WHY?!?!?!? >>> >>> It seems perverse to go to the overhead of building a complete WIMP >>> user interface to do >>> >>> for ( int c = System.in.read(); c> -1; c = System.in.read()) { >>> char ch = (char) c; >>> /* now do something with ch */ >>> } >>> >>> In practice something like the following would be more useful: >>> >>> string readLineFromStdin() { >>> StringBuffer buffy = new StringBuffer(); bool reading = true; >>> >>> while ( reading) { >>> int c = System.in.read(); >>> >>> switch (c) { >>> case 10: >>> case 13: >>> case -1: >>> reading = false; >>> break; >>> default: >>> buffy.append( (char)c); >>> break; >>> } >>> } >>> return buffy.toString(); >>> } >>> >>> although in anything but the simplest utility programs you'd probably >>> do something a touch more sophisticated than that. >> >>No - you would do it a lot simpler than that in any program. >> >>BufferedReader or Scanner will save a lot of code. > > As I'm finding out, thanks. However, would the above teach me anything > about 'how it works' even if it is overly wordy? If I hadn't thought it would, I wouldn't have posted it... Underneath anything that reads from an input stream in Java is that basic method read() returning an int, -1 in the case of end of stream, otherwise 0-255 indicating the byte read. This may seem odd given that Java characters are sixteen bit, but remember that this language was designed as a language primarily for small embedded systems. At it's lowest levels it's quite simple and even primitive. -- ;; Semper in faecibus sumus, sole profundam variat
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Newbie again. "Java Keyboard input" is a failure as a google search. What isn't? Next: Your Helping Heart |