From: Tom Anderson on
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
From: Andreas Leitgeb on
Stefan Ram <ram(a)zedat.fu-berlin.de> 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.

And the javadoc on java.lang.System's field "in" is another approach.

From: Simon Brooke on
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.

--

;; Semper in faecibus sumus, sole profundam variat

From: Simon Brooke on
On Sun, 18 Jul 2010 12:45:26 +0000, 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.
>
> (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

From: markspace on
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.