Hi all.
As some of you may remember from a couple of other recent newbie
posts, I'm trying to teach myself, slowly, by book. As in previous
posts can you give me some guidance please?
Currently I want to learn about reading input from the keyboard. Doing
a google search for "Java keyboard input" and similar hasn't brought
me to the nirvana I'd like. I know it must be there, but where?
What I do get are hits such as:
http://wiki.erland.homeip.net/index.php/Java_Tutorial_Lesson_12:_Keyboard_input
which is part way through something else,
http://www.pp.rhul.ac.uk/~george/PH2150/html/node13.html
This says what, but not why,
http://www.brighthub.com/internet/web-development/articles/16220.aspx
3d on the web... and very high on the list of Google responses.
Of course, working through them I'm sure I can come to some sort of
realisation of what it entails, but I'm looking for the full
explanation.
My book, Head First Java, doesn't have "keyboard" in the index. The
only "Input" in the index is "InputStreamReader" which is used in
reference to reading data from a socket.
[An "aha!" moment] I've just found a document in the Java Tutorials
called basic i/o. This may be my nirvana but in case not I'll post
this anyway.
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.
Thanks in advance.
Mike.
|
|
0
|
|
|
|
Reply
|
Mike
|
7/18/2010 10:50:45 AM |
|
Mike Barnard <m.barnard.trousers@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.
|
|
0
|
|
|
|
Reply
|
ram
|
7/18/2010 10:59:06 AM
|
|
On 18 Jul 2010 10:59:06 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote:
>Mike Barnard <m.barnard.trousers@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.
I'm still on the absolute basics, and Swing is a subject still to be
touched. Thanks anyway, it's bookmarked for the future!
Mike.
|
|
0
|
|
|
|
Reply
|
Mike
|
7/18/2010 11:23:39 AM
|
|
On Sun, 18 Jul 2010, Mike Barnard wrote:
> As some of you may remember from a couple of other recent newbie posts,
> I'm trying to teach myself, slowly, by book. As in previous posts can
> you give me some guidance please?
>
> Currently I want to learn about reading input from the keyboard. Doing
> a google search for "Java keyboard input" and similar hasn't brought
> me to the nirvana I'd like. I know it must be there, but where?
I assume you're not talking about doing this in a GUI, but from the
command line. In that case, it might help to know that this interface
is usually called the 'console', and occasionally the 'terminal'.
Searching for 'java console input' should be more helpful.
I'll give a further steer that the two things you're interested in are
System.in and java.util.Scanner.
> What I do get are hits such as:
>
> http://wiki.erland.homeip.net/index.php/Java_Tutorial_Lesson_12:_Keyboard_input
> which is part way through something else,
>
> http://www.pp.rhul.ac.uk/~george/PH2150/html/node13.html
> This says what, but not why,
What do you mean by that?
tom
--
Virtually everything you touch has been mined. -- Prof Keith Atkinson
|
|
0
|
|
|
|
Reply
|
Tom
|
7/18/2010 12:04:29 PM
|
|
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> Mike Barnard <m.barnard.trousers@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.
And the javadoc on java.lang.System's field "in" is another approach.
|
|
0
|
|
|
|
Reply
|
Andreas
|
7/18/2010 12:16:54 PM
|
|
Andreas Leitgeb <avl@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.
(There are several that read the command line arguments as in
main( final java.lang.String[] args )
, but this does not use System.in.)
So why should I recommend something that I deem to be used
hardly ever in applied programming?
|
|
0
|
|
|
|
Reply
|
ram
|
7/18/2010 12:45:26 PM
|
|
On Sun, 18 Jul 2010 11:50:45 +0100, Mike Barnard
<m.barnard.trousers@thunderin.co.uk> wrote, quoted or indirectly
quoted someone who said :
>Hi all.
>
>As some of you may remember from a couple of other recent newbie
>posts, I'm trying to teach myself, slowly, by book. As in previous
>posts can you give me some guidance please?
>
>Currently I want to learn about reading input from the keyboard. Doing
>a google search for "Java keyboard input" and similar hasn't brought
>me to the nirvana I'd like. I know it must be there, but where?
>
>What I do get are hits such as:
>
>http://wiki.erland.homeip.net/index.php/Java_Tutorial_Lesson_12:_Keyboard_input
>which is part way through something else,
>
>http://www.pp.rhul.ac.uk/~george/PH2150/html/node13.html
>This says what, but not why,
>
>http://www.brighthub.com/internet/web-development/articles/16220.aspx
>3d on the web... and very high on the list of Google responses.
>
>Of course, working through them I'm sure I can come to some sort of
>realisation of what it entails, but I'm looking for the full
>explanation.
>
>My book, Head First Java, doesn't have "keyboard" in the index. The
>only "Input" in the index is "InputStreamReader" which is used in
>reference to reading data from a socket.
>
>[An "aha!" moment] I've just found a document in the Java Tutorials
>called basic i/o. This may be my nirvana but in case not I'll post
>this anyway.
>
>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.
>
>Thanks in advance.
>
>Mike.
see http://mindprod.com/products1.html#KEYPLAYER
for how to read the keyboard at a low level.
Normally you do it with components such as JTextField and brethren.
See http://mindprod.com/jgloss/jtextfield.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
|
|
0
|
|
|
|
Reply
|
see_website (4858)
|
7/18/2010 1:19:17 PM
|
|
Mike Barnard writes:
> Hi all.
>
> As some of you may remember from a couple of other recent newbie
> posts, I'm trying to teach myself, slowly, by book. As in previous
> posts can you give me some guidance please?
>
> Currently I want to learn about reading input from the keyboard. Doing
> a google search for "Java keyboard input" and similar hasn't brought
> me to the nirvana I'd like. I know it must be there, but where?
You might find something relevant with "java readline" or "java
editline" or "java jline". These lead to a couple of libraries that
provide an editable command line, including history.
I used one of these a few years ago - jline, I think - on some
GNU/Linux system.
|
|
0
|
|
|
|
Reply
|
Jussi
|
7/18/2010 1:35:53 PM
|
|
On Sun, 18 Jul 2010 10:59:06 +0000, Stefan Ram wrote:
> Mike Barnard <m.barnard.trousers@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.
--
;; Semper in faecibus sumus, sole profundam variat
|
|
0
|
|
|
|
Reply
|
Simon
|
7/18/2010 3:52:40 PM
|
|
On Sun, 18 Jul 2010 11:50:45 +0100, Mike Barnard
<m.barnard.trousers@thunderin.co.uk> wrote:
>Hi all.
>
Another lazy sunday afternoon post. My thanks to one and all. At the
moment I am learning via the command line and have done no work with a
GUI. I'm looking for stuff like Basic's "Input" command or Delphi's
"OnKeyPress" maybe.
I have a simple, single class which I'm using to play with creating
objects, for my education. I want a method that gets a keypress from
the user (Just a single character) and will allow me to do stuff
depending on what it is.
Console isn't in the Head First book either, but searching the web for
that brings up a different list of options, so I'll play on.
Thanks again.
|
|
0
|
|
|
|
Reply
|
Mike
|
7/18/2010 3:53:58 PM
|
|
On Sun, 18 Jul 2010 12:45:26 +0000, Stefan Ram wrote:
> Andreas Leitgeb <avl@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.
>
> (There are several that read the command line arguments as in
>
> main( final java.lang.String[] args )
>
> , but this does not use System.in.)
>
> So why should I recommend something that I deem to be used hardly ever
> in applied programming?
I not infrequently build little utilities - often for testing - which
read from standard input - which can be the keyboard, even if in practice
it often isn't.
--
;; Semper in faecibus sumus, sole profundam variat
|
|
0
|
|
|
|
Reply
|
Simon
|
7/18/2010 3:54:30 PM
|
|
On 18 Jul 2010 12:45:26 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote:
>Andreas Leitgeb <avl@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.
>
> (There are several that read the command line arguments as in
>
>main( final java.lang.String[] args )
>
> , but this does not use System.in.)
>
> So why should I recommend something that I deem to be used
> hardly ever in applied programming?
Thanks for the warning, I won't go that way just yet.
|
|
0
|
|
|
|
Reply
|
Mike
|
7/18/2010 3:54:45 PM
|
|
On Sun, 18 Jul 2010 13:04:29 +0100, Tom Anderson
<twic@urchin.earth.li> wrote:
>On Sun, 18 Jul 2010, Mike Barnard wrote:
>
>> As some of you may remember from a couple of other recent newbie posts,
>> I'm trying to teach myself, slowly, by book. As in previous posts can
>> you give me some guidance please?
>>
>> Currently I want to learn about reading input from the keyboard. Doing
>> a google search for "Java keyboard input" and similar hasn't brought
>> me to the nirvana I'd like. I know it must be there, but where?
>
>I assume you're not talking about doing this in a GUI, but from the
Correctamondo...
>command line. In that case, it might help to know that this interface
>is usually called the 'console', and occasionally the 'terminal'.
>Searching for 'java console input' should be more helpful.
Done, more tutorials to choose from. I might have some idea of what
I'm talking about eventually!
>I'll give a further steer that the two things you're interested in are
>System.in and java.util.Scanner.
Someone above says system.in isn't used much. Is this because it's too
low level and everyone else uses GUI stuff?
>> What I do get are hits such as:
>>
>> http://wiki.erland.homeip.net/index.php/Java_Tutorial_Lesson_12:_Keyboard_input
>> which is part way through something else,
>>
>> http://www.pp.rhul.ac.uk/~george/PH2150/html/node13.html
>> This says what, but not why,
>
>What do you mean by that?
The second one? I mean it shows some code and says "do it like this".
It doesn't explain why this is any better or worse than any other way.
There is no tutorial, just parrot fashion "do it". (But I haven't yet
read it in depth, SIMBW.)
>tom
I thought Java was case sensitive? :)
|
|
0
|
|
|
|
Reply
|
Mike
|
7/18/2010 3:59:28 PM
|
|
On Sun, 18 Jul 2010 06:19:17 -0700, Roedy Green
<see_website@mindprod.com.invalid> wrote:
>On Sun, 18 Jul 2010 11:50:45 +0100, Mike Barnard
><m.barnard.trousers@thunderin.co.uk> wrote, quoted or indirectly
>quoted someone who said :
>
>>Hi all.
>>
>>As some of you may remember from a couple of other recent newbie
>>posts, I'm trying to teach myself, slowly, by book. As in previous
>>posts can you give me some guidance please?
>>
>>Currently I want to learn about reading input from the keyboard. Doing
>>a google search for "Java keyboard input" and similar hasn't brought
>>me to the nirvana I'd like. I know it must be there, but where?
>>
>>What I do get are hits such as:
>>
>>http://wiki.erland.homeip.net/index.php/Java_Tutorial_Lesson_12:_Keyboard_input
>>which is part way through something else,
>>
>>http://www.pp.rhul.ac.uk/~george/PH2150/html/node13.html
>>This says what, but not why,
>>
>>http://www.brighthub.com/internet/web-development/articles/16220.aspx
>>3d on the web... and very high on the list of Google responses.
>>
>>Of course, working through them I'm sure I can come to some sort of
>>realisation of what it entails, but I'm looking for the full
>>explanation.
>>
>>My book, Head First Java, doesn't have "keyboard" in the index. The
>>only "Input" in the index is "InputStreamReader" which is used in
>>reference to reading data from a socket.
>>
>>[An "aha!" moment] I've just found a document in the Java Tutorials
>>called basic i/o. This may be my nirvana but in case not I'll post
>>this anyway.
>>
>>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.
>>
>>Thanks in advance.
>>
>>Mike.
>
>see http://mindprod.com/products1.html#KEYPLAYER
>for how to read the keyboard at a low level.
I find Mindprod but not Keyplayer...
>Normally you do it with components such as JTextField and brethren.
>See http://mindprod.com/jgloss/jtextfield.html
I see this. More learning... hello brain, take this!
Thanks.
|
|
0
|
|
|
|
Reply
|
m.barnard.trousers (12)
|
7/18/2010 4:07:58 PM
|
|
On 18 Jul 2010 16:35:53 +0300, Jussi Piitulainen
<jpiitula@ling.helsinki.fi> wrote:
>Mike Barnard writes:
>
>> Hi all.
>>
>> As some of you may remember from a couple of other recent newbie
>> posts, I'm trying to teach myself, slowly, by book. As in previous
>> posts can you give me some guidance please?
>>
>> Currently I want to learn about reading input from the keyboard. Doing
>> a google search for "Java keyboard input" and similar hasn't brought
>> me to the nirvana I'd like. I know it must be there, but where?
>
>You might find something relevant with "java readline" or "java
>editline" or "java jline". These lead to a couple of libraries that
>provide an editable command line, including history.
>
>I used one of these a few years ago - jline, I think - on some
>GNU/Linux system.
Thanks.
|
|
0
|
|
|
|
Reply
|
Mike
|
7/18/2010 4:08:25 PM
|
|
Mike Barnard wrote:
> My book, Head First Java, doesn't have "keyboard" in the index. The
> only "Input" in the index is "InputStreamReader" which is used in
> reference to reading data from a socket.
The standard input (from the terminal) is an input stream just like a
socket. So unfortunately this is exactly what you want to use, if you
are trying to learn from the ground up. See "Standard Streams" here:
<http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/essential/io/cl.html>
Esp. this bit:
"By contrast, System.in is a byte stream with no character stream
features. To use Standard Input as a character stream, wrap System.in in
InputStreamReader.
InputStreamReader cin = new InputStreamReader(System.in);"
So now you can use all the techniques associated with InputStream to
read user terminal input. You should probably look at BufferedReader,
which is the next step up the food chain for stream input from
InputStreamReader.
As mentioned, there's also the Scanner class, which is a step up from
BufferedReader, and there is also the console (scroll down a bit on the
page linked to above) which adds some capabilities not normally found in
streams.
|
|
0
|
|
|
|
Reply
|
markspace
|
7/18/2010 4:16:53 PM
|
|
On 07/18/2010 11:54 AM, Mike Barnard wrote:
> On 18 Jul 2010 12:45:26 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
> wrote:
>
>> Andreas Leitgeb<avl@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.
>>
>> (There are several that read the command line arguments as in
>>
>> main( final java.lang.String[] args )
>>
>> , but this does not use System.in.)
>>
>> So why should I recommend something that I deem to be used
>> hardly ever in applied programming?
>
> Thanks for the warning, I won't go that way just yet.
False warning. System.in is rather commonly used, though perhaps not as much
as other inputs, and GUIs use keyed and mouse input quite a bit.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
7/18/2010 4:45:25 PM
|
|
Simon Brooke wrote:
> 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()) {
Do not use TAB characters to indent Usenet posts. Use spaces. A maximum of
four per indent level suits Usenet readability best.
> 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();
'StringBuffer'? Really?
> bool reading = true;
>
> while ( reading) {
> int c = System.in.read();
>
> switch (c) {
> case 10:
We assume the encoding here.
> 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.
Yeah, like reading an entire String at once, perhaps with 'Scanner'.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
7/18/2010 4:48:23 PM
|
|
Mike Barnard wrote:
> On Sun, 18 Jul 2010 13:04:29 +0100, Tom Anderson
> <twic@urchin.earth.li> wrote:
>
>> On Sun, 18 Jul 2010, Mike Barnard wrote:
>>
>>> As some of you may remember from a couple of other recent newbie posts,
>>> I'm trying to teach myself, slowly, by book. As in previous posts can
>>> you give me some guidance please?
>>>
>>> Currently I want to learn about reading input from the keyboard. Doing
>>> a google search for "Java keyboard input" and similar hasn't brought
>>> me to the nirvana I'd like. I know it must be there, but where?
>> I assume you're not talking about doing this in a GUI, but from the
>
> Correctamondo...
>
>> command line. In that case, it might help to know that this interface
>> is usually called the 'console', and occasionally the 'terminal'.
>> Searching for 'java console input' should be more helpful.
>
> Done, more tutorials to choose from. I might have some idea of what
> I'm talking about eventually!
>
>> I'll give a further steer that the two things you're interested in are
>> System.in and java.util.Scanner.
>
> Someone above says system.in isn't used much. Is this because it's too
> low level and everyone else uses GUI stuff?
[ SNIP ]
You won't typically use it unless you are really reading keyboard input.
I don't mean command-line arguments, but actual points in your executing
program where you prompt the user and ask them to type stuff in.
There is another fairly common situation where System.in figures
prominently, and that's
Process proc = Runtime.getRuntime().exec("command");
or variants thereof. IOW, executing a native command on your system. The
way in which you communicate with that process is with its System.in,
System.out and System.err streams.
AHS
--
The cook was a good cook as cooks go; and as cooks go, she went.
-- HH Munro
|
|
0
|
|
|
|
Reply
|
Arved
|
7/18/2010 7:25:35 PM
|
|
On Sun, 18 Jul 2010, Mike Barnard wrote:
> On Sun, 18 Jul 2010 13:04:29 +0100, Tom Anderson
> <twic@urchin.earth.li> wrote:
>
>> I'll give a further steer that the two things you're interested in are
>> System.in and java.util.Scanner.
>
> Someone above says system.in isn't used much. Is this because it's too
> low level and everyone else uses GUI stuff?
It's because very there are very few interactive command-line programs
written in java. The most common interface to a java program is an HTTP
port, and the second most common is probably a GUI. Other forms of network
interface make up the rest of the hit parade, leaving the console rather
far down the list.
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, and Stefan's
point, while generally true, does not apply to you.
>> tom
>
> I thought Java was case sensitive? :)
Not nearly so as Lew.
tom
--
YOUR MIND IS A NIGHTMARE THAT HAS BEEN EATING YOU: NOW EAT YOUR MIND. --
Kathy Acker, Empire of the Senseless
|
|
0
|
|
|
|
Reply
|
twic (2083)
|
7/18/2010 10:36:23 PM
|
|
Tom Anderson wrote:
>>> tom
Mike Barnard wrote:
>> I thought Java was case sensitive? :)
That isn't Java.
Tom Anderson wrote:
> Not nearly so as Lew.
>
> tom
tom is not correct. I'm marginally less case sensitive than Java. I
certainly would not presume to tell tom how to spell his own name.
I'm sensitive to certain fundamental case issues, like how to spell "Java" and
other proper nouns, and of course, the word "I" in English. Spelling it "i"
is just tomfoolery.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
7/19/2010 1:37:14 AM
|
|
On 18-07-2010 08:45, Stefan Ram wrote:
> Andreas Leitgeb<avl@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
|
|
0
|
|
|
|
Reply
|
ISO
|
7/19/2010 2:46:45 AM
|
|
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@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
|
|
0
|
|
|
|
Reply
|
UTF
|
7/19/2010 2:48:28 AM
|
|
Tom Anderson <twic@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. -- And then,
suddenly, a Swing-program to do the same, might not be much
more difficult anymore.
(However, to apply Swing beyond toy programs, one should
know about model-view-controller-separation and event-based
design.)
|
|
0
|
|
|
|
Reply
|
ram
|
7/19/2010 6:53:42 AM
|
|
On Sun, 18 Jul 2010 22:48:28 -0400, Arne Vajh�j <arne@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@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?
>Arne
|
|
0
|
|
|
|
Reply
|
Mike
|
7/19/2010 6:58:28 AM
|
|
Simon Brooke <stillyet+nntp@googlemail.com> writes:
>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()) {
The OP said �reading input from the keyboard�.
�System.in� might be associated with any file or device
whatsoever on program start-up, beyond the control of the
Java-program running. So, the program can not know whether
�System.in� is connected with the keyboard at all.
This is not just a remote possibility. Instead �redirecting�
System.in is used often.
A JTextField /is/ certainly associated with the keyboard,
which is what was asked for: �reading input from the keyboard�.
|
|
0
|
|
|
|
Reply
|
ram
|
7/19/2010 7:18:15 AM
|
|
On Mon, 19 Jul 2010, Stefan Ram wrote:
> Tom Anderson <twic@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!
|
|
0
|
|
|
|
Reply
|
Tom
|
7/19/2010 9:06:44 AM
|
|
Tom Anderson <twic@urchin.earth.li> writes:
>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?
Sorry, I did confuse this with �System.out�, which /has/ an
encoding as a PrintStream.
However, a part of my point still is valid: When using
�System.in�, the programmer gets raw bytes and needs to be
able to choose the correct encoding himself, when wrapping
�System.in� into other objects that accept an encoding.
And when he then wants to write his German Umlauts to the
console, he sometimes needs to adjust the output encoding.
Doing this even portably might be a challenging task.
Swing should already be set up to accept any common
Unicode-character from the keyboard and display any common
Unicode-character in the GUI, well at least every character
from ISO-8859-1.
|
|
0
|
|
|
|
Reply
|
ram
|
7/19/2010 9:37:19 AM
|
|
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 |
|
|
0
|
|
|
|
Reply
|
Martin
|
7/19/2010 11:21:13 AM
|
|
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@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@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
|
|
0
|
|
|
|
Reply
|
Simon
|
7/19/2010 11:33:53 AM
|
|
ram@zedat.fu-berlin.de (Stefan Ram) writes:
>Swing should already be set up to accept any common
>Unicode-character from the keyboard and display any common
>Unicode-character in the GUI, well at least every character
>from ISO-8859-1.
public class Main implements java.lang.Runnable, java.awt.event.ActionListener
{ static final javax.swing.JFrame frame = new javax.swing.JFrame();
static final javax.swing.JTextField input = new javax.swing.JTextField();
public void run()
{ input.addActionListener( this );
frame.add( input ); frame.pack(); frame.setVisible(true); }
public void actionPerformed( final java.awt.event.ActionEvent event )
{ javax.swing.JOptionPane.showMessageDialog( frame, input.getText() );
System.exit( 0 ); }
public static void main( final String[] args )
{ javax.swing.SwingUtilities.invokeLater( new Main() ); }}
1. Text can be entered (edited) and terminated with the return key.
2. Text will be displayed in a message box.
3. User presses [OK].
4. Program terminates.
|
|
0
|
|
|
|
Reply
|
ram
|
7/19/2010 12:43:42 PM
|
|
On 19-07-2010 03:18, Stefan Ram wrote:
> Simon Brooke<stillyet+nntp@googlemail.com> writes:
>> 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()) {
>
> The OP said �reading input from the keyboard�.
>
> �System.in� might be associated with any file or device
> whatsoever on program start-up, beyond the control of the
> Java-program running. So, the program can not know whether
> �System.in� is connected with the keyboard at all.
>
> This is not just a remote possibility. Instead �redirecting�
> System.in is used often.
>
> A JTextField /is/ certainly associated with the keyboard,
> which is what was asked for: �reading input from the keyboard�.
It is absolutely not certain that JTextField is associated with
the keyboard.
Sure you can not redirect it. But you can have a context with
no Swing support. Very easily.
Arne
|
|
0
|
|
|
|
Reply
|
ISO
|
7/19/2010 11:20:16 PM
|
|
On 19-07-2010 02:58, Mike Barnard wrote:
> On Sun, 18 Jul 2010 22:48:28 -0400, Arne Vajh�j<arne@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@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?
No.
Not unless you count "learning good 1980 C code programming
using Java as language".
Arne
|
|
0
|
|
|
|
Reply
|
ISO
|
7/19/2010 11:21:15 PM
|
|
On 19-07-2010 07:33, Simon Brooke wrote:
> 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@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@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.
The fact that chars are 16 bit does not have much relevance for
a method returning bytes (which are 8 bit).
Arne
|
|
0
|
|
|
|
Reply
|
UTF
|
7/19/2010 11:22:36 PM
|
|
On 19-07-2010 02:53, Stefan Ram wrote:
> Tom Anderson<twic@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. -- And then,
> suddenly, a Swing-program to do the same, might not be much
> more difficult anymore.
As a rule of thumb then I would say that the need to go beyond
plan US ASCII makes GUI (Swing or SWT) attractive compared
to console.
But I still think that it makes sense to start with console.
Arne
|
|
0
|
|
|
|
Reply
|
ISO
|
7/19/2010 11:24:37 PM
|
|
|
34 Replies
335 Views
(page loaded in 0.303 seconds)
|