From: Lew on
On 07/18/2010 11:54 AM, Mike Barnard wrote:
> On 18 Jul 2010 12:45:26 GMT, ram(a)zedat.fu-berlin.de (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?
>
> 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
From: Lew on
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
From: Arved Sandstrom on
Mike Barnard wrote:
> On Sun, 18 Jul 2010 13:04:29 +0100, Tom Anderson
> <twic(a)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
From: Tom Anderson on
On Sun, 18 Jul 2010, Mike Barnard wrote:

> On Sun, 18 Jul 2010 13:04:29 +0100, Tom Anderson
> <twic(a)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
From: Lew on
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