Jump to page: 1 25  
Page
Thread overview
OT - scanf in Java
Jul 26, 2004
Arcane Jill
Jul 26, 2004
Andy Friesen
Jul 26, 2004
Berin Loritsch
Jul 26, 2004
parabolis
Jul 26, 2004
Berin Loritsch
Jul 26, 2004
Arcane Jill
Jul 26, 2004
Sean Kelly
Unicode Digits (was OT - scanf in Java)
Jul 27, 2004
Arcane Jill
Jul 27, 2004
Sean Kelly
Jul 27, 2004
parabolis
Jul 27, 2004
Arcane Jill
Jul 27, 2004
parabolis
Jul 27, 2004
Arcane Jill
Jul 27, 2004
parabolis
Jul 28, 2004
Sean Kelly
Jul 28, 2004
parabolis
String class (was OT - scanf in Java)
Jul 28, 2004
Arcane Jill
Jul 28, 2004
J C Calvarese
Jul 28, 2004
Arcane Jill
Jul 28, 2004
parabolis
Jul 28, 2004
parabolis
Jul 28, 2004
Arcane Jill
Jul 28, 2004
Berin Loritsch
Jul 28, 2004
parabolis
Jul 28, 2004
Ben Hinkle
Strings (was OT - scanf in Java)
Jul 28, 2004
Arcane Jill
Jul 28, 2004
parabolis
Strings (was: scanf in Java)
Jul 28, 2004
Arcane Jill
Jul 27, 2004
parabolis
Jul 27, 2004
Berin Loritsch
Jul 27, 2004
parabolis
Jul 27, 2004
Berin Loritsch
Jul 27, 2004
parabolis
Jul 27, 2004
Arcane Jill
Jul 27, 2004
Berin Loritsch
Jul 27, 2004
parabolis
Jul 28, 2004
Sean Kelly
Jul 28, 2004
parabolis
Jul 28, 2004
Sean Kelly
Jul 26, 2004
Sha Chancellor
Jul 27, 2004
parabolis
July 26, 2004
I realize that this is not a Java forum, but I'm trying to get a feel for how D compares to other things. I want to know, how does one get a line of input from the console in Java? I've written some insignificant amount of code in Java in the past, but none of it ever needed to get a line of input from the console.

Here's the reference program - written in C++. Just as an exercise, I'm comparing this with other languages. (I know it's not really a fair test, but what the hell?)

#    #include <iostream>
#    #include <string>
#
#    void main()
#    {
#        std::string s;
#        cin << s;
#        cout >> s >> endl;
#    }

I can translate that into most other languages easily - but for Java, I'm stuck. How would you do this? Especially, how would you do this without using any deprecated functions?

(D doesn't do very well at this one, incidently, but that's just a temporary phase. Things will obviously get better when stream support improves and we get a native-D scanf replacement, both of which, I gather, are underway).

Arcane Jill



July 26, 2004
Arcane Jill wrote:

> I realize that this is not a Java forum, but I'm trying to get a feel for how D
> compares to other things. I want to know, how does one get a line of input from
> the console in Java? I've written some insignificant amount of code in Java in
> the past, but none of it ever needed to get a line of input from the console.
> 
> Here's the reference program - written in C++. Just as an exercise, I'm
> comparing this with other languages. (I know it's not really a fair test, but
> what the hell?)
> 
> #    #include <iostream>
> #    #include <string>
> #
> #    void main()
> #    {
> #        std::string s;
> #        cin << s;
> #        cout >> s >> endl;
> #    }
> 
> I can translate that into most other languages easily - but for Java, I'm stuck.
> How would you do this? Especially, how would you do this without using any
> deprecated functions?
> 
> (D doesn't do very well at this one, incidently, but that's just a temporary
> phase. Things will obviously get better when stream support improves and we get
> a native-D scanf replacement, both of which, I gather, are underway).

It's been quite awhile, but I think it goes something like this:

    import java.io.*;

    public class TheMainClass {
        public static void main(String[] args) {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            String s = br.readLine();
            System.out.writeLine(s);
        }
    }

Beating java on this one isn't very hard. :)

 -- andy
July 26, 2004
Arcane Jill wrote:

> I realize that this is not a Java forum, but I'm trying to get a feel for how D
> compares to other things. I want to know, how does one get a line of input from
> the console in Java? I've written some insignificant amount of code in Java in
> the past, but none of it ever needed to get a line of input from the console.
> 
> Here's the reference program - written in C++. Just as an exercise, I'm
> comparing this with other languages. (I know it's not really a fair test, but
> what the hell?)
> 
> #    #include <iostream>
> #    #include <string>
> #
> #    void main()
> #    {
> #        std::string s;
> #        cin << s;
> #        cout >> s >> endl;
> #    }
> 
> I can translate that into most other languages easily - but for Java, I'm stuck.
> How would you do this? Especially, how would you do this without using any
> deprecated functions?
> 
> (D doesn't do very well at this one, incidently, but that's just a temporary
> phase. Things will obviously get better when stream support improves and we get
> a native-D scanf replacement, both of which, I gather, are underway).

You have some options.  In Java 1.5, there is a new Scanner class--since
I haven't played with it much, I will have to stick with the older
methods.

The System class holds a reference to stdin and stdout as System.in and
System.out respectively.

Using the System.in, you can wrap whatever input streams/readers you
need to parse the input as expected.
July 26, 2004
Arcane Jill wrote:
> I realize that this is not a Java forum, but I'm trying to get a feel for how D
> compares to other things. I want to know, how does one get a line of input from
> the console in Java? I've written some insignificant amount of code in Java in
> the past, but none of it ever needed to get a line of input from the console.

Andy Friesen's code solves the problem, but since you are comparing languages I would suggest you take a stroll through the java.io classes to get a feel for how the IO library works. It is really well done in my opinion and an shining example of what really well done OO code looks like.

The Java API specs are available from
    http://java.sun.com/j2se/1.4.2/docs/api/


The abstract class InputStream defines a small number of fundamental operations that conceptually define a Stream. The _only_ abstract function is

    abstract int read();

This is the only function a subclass needs to define to have the whole InputStream repotoire available. Some InputStream subclasses provide data from sources like files, socket connections and in memory data structures:

    FileInputStream in java.io
    SocketInputStream in java.net
    StringInputStream in java.io


The rest of the functions have meainingful default behavior. So reading bytes into an array in the general case is handled in InputStream by:

    int read(byte buf, int off, int len)  (not abstract!)

Interesting intermediate behavior is obtained by passing an InputStream subclass to other InputStreams. Interesting intermediate behavior includes:

    Buffering in java.io.Buffered___Stream
    En/De-cryption in javax.crypto.Cipher___Stream
    Compression in java.util.zip.Inflater___Stream
    Decompression in java.util.zip.Deflater___Stream
    Digesting (eg CRC32) in java.util.zip.Checked___Stream


Interesting final behavior (ie the reason you opened the stream to begin with...) includes:

    Read\Write general Data in java.io.Data___Stream
    Read\Write Ojects in java.io.Object___Stream
    Read\Write zip file in java.util.zip.Zip___Stream

The end result is mixing and matching Streams to suit your needs.

Say you want to read something in. You only need to answer 3 questions:

1) From where? File, Socket, Data Structure, etc...
2) How? Buffered, Encrypted, Digested, etc...
3) What kind? Data, Object, etc...

Say you want to read in
  1) from a File
  2) compressed, digested and buffed
  3) Data

That would be:

DataInputStream input =
  new DataInputStream(
    new CheckedInputStream(
      new DeflaterInputStream(
        new BufferedInputStream(
          new FileInputStream("filename.ext")
        )
      )
    )
  );

In this case input will buffer, then decompress, then digest anything you read from filename.ext.


An item to pay particular attention to is the Object___Stream. When combined with Socket___Stream you can send Objects to (or read object from) a TCP connection.

If you write a java.lang.Runnable object to an ObjectOutputStream which then sends it to a server the server can cast object read to Runnable and then start a new thread which calls the objects run() method. Thus it is possible to start a Server and leave it running then later write new code and send it (code the Server has never seen before).


July 26, 2004
parabolis wrote:

> Arcane Jill wrote:
> 
>> I realize that this is not a Java forum, but I'm trying to get a feel for how D
>> compares to other things. I want to know, how does one get a line of input from
>> the console in Java? I've written some insignificant amount of code in Java in
>> the past, but none of it ever needed to get a line of input from the console.
> 
> 
> Andy Friesen's code solves the problem, but since you are comparing languages I would suggest you take a stroll through the java.io classes to get a feel for how the IO library works. It is really well done in my opinion and an shining example of what really well done OO code looks like.
> 
> The Java API specs are available from
>     http://java.sun.com/j2se/1.4.2/docs/api/

Another place to look, if you want to see how they are planning on improving things is here:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
http://java.sun.com/j2se/1.5.0/docs/api/
July 26, 2004
In article <ce3k0a$1co1$1@digitaldaemon.com>,
 parabolis <parabolis@softhome.net> wrote:

> DataInputStream input =
>    new DataInputStream(
>      new CheckedInputStream(
>        new DeflaterInputStream(
>          new BufferedInputStream(
>            new FileInputStream("filename.ext")
>          )
>        )
>      )
>    );

Do they have a DecaffinateInputStream and a DefatInputStream class also by any chance?  I heard they do.
July 26, 2004
In article <ce3o9u$1enc$1@digitaldaemon.com>, Berin Loritsch says...

>Another place to look, if you want to see how they are planning on improving things is here:
>
>http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Hey, cool. They can parse non-Latin digits.

# NonASCIIDigit  ::  	= A non-ASCII character c for which Character.isDigit(c)
returns true

So, Arab digits, Bengali digits, no problem.

Not sure how they'd cope with Osmanya digits though - these have codepoints U+0104A0 to U+0104A9 inclusive - too big to fit into a Java char.

We'll have this in D eventually, but we won't stop at wchars.

Arcane Jill



July 26, 2004
In article <ce3pln$1fds$1@digitaldaemon.com>, Arcane Jill says...
>
>In article <ce3o9u$1enc$1@digitaldaemon.com>, Berin Loritsch says...
>
>>Another place to look, if you want to see how they are planning on improving things is here:
>>
>>http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
>
>Hey, cool. They can parse non-Latin digits.
>
># NonASCIIDigit  ::  	= A non-ASCII character c for which Character.isDigit(c)
>returns true
>
>So, Arab digits, Bengali digits, no problem.
>
>Not sure how they'd cope with Osmanya digits though - these have codepoints U+0104A0 to U+0104A9 inclusive - too big to fit into a Java char.
>
>We'll have this in D eventually, but we won't stop at wchars.

I've been wondering about this.  readf (was scanf) still uses some lame shortcuts like "x - '0'" but that wouldn't be too terribly hard to fix.  I don't suppose the unicode isdigit function currently supports these numbering schemes? Also, is it reasonable to assume that every numbering scheme is base 10?  I'd certainly think so, but I suppose it's worth asking.


Sean


July 27, 2004
Sha Chancellor wrote:

> In article <ce3k0a$1co1$1@digitaldaemon.com>,
>  parabolis <parabolis@softhome.net> wrote:
> 
> 
>>DataInputStream input =
>>   new DataInputStream(
>>     new CheckedInputStream(
>>       new DeflaterInputStream(
>>         new BufferedInputStream(
>>           new FileInputStream("filename.ext")
>>         )
>>       )
>>     )
>>   );
> 
> 
> Do they have a DecaffinateInputStream and a DefatInputStream class also by any chance?  I heard they do.

lol
I have surely never used them myself. Maybe java.beans somewhere... :P
July 27, 2004
Berin Loritsch wrote:
> 
> Another place to look, if you want to see how they are planning on improving things is here:
> 
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
> http://java.sun.com/j2se/1.5.0/docs/api/

Sadly the one thing I really want them to implement will never happen - unsigned primitive types.
« First   ‹ Prev
1 2 3 4 5