Newbie again. "Java Keyboard input" is a failure as a google search. What isn't?

M

Mike Barnard

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.
 
S

Stefan Ram

Mike Barnard said:
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.
 
M

Mike Barnard

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.
 
T

Tom Anderson

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
 
A

Andreas Leitgeb

Stefan Ram said:
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.
 
S

Stefan Ram

Andreas Leitgeb said:
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?
 
R

Roedy Green

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
 
J

Jussi Piitulainen

Mike 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?

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.
 
S

Simon Brooke

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.
 
M

Mike Barnard


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.
 
S

Simon Brooke

Andreas Leitgeb said:
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.
 
M

Mike Barnard

Andreas Leitgeb said:
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.
 
M

Mike Barnard

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 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.)

I thought Java was case sensitive? :)
 
M

Mike Barnard

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.
 
M

Mike Barnard

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.
 
M

markspace

Mike said:
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.
 
L

Lew

Andreas Leitgeb said:
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.
 
L

Lew

Simon said:
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'.
 
A

Arved Sandstrom

Mike said:
Done, more tutorials to choose from. I might have some idea of what
I'm talking about eventually!


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
 
T

Tom Anderson

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.
I thought Java was case sensitive? :)

Not nearly so as Lew.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top