reading from console, InputStreamReader etc.

Discussion in 'Java' started by mehafi@gmail.com, Aug 2, 2007.

  1. Guest

    Hi,
    I've got 2 question:
    1) I'd like to read a double value from console, so I wrote:

    try{
    char c = new char[100];
    InputStreamReader isr = new InputStreamReader(System.in);
    isr.read(c);
    String s = new String(c);
    double d = Double.parseDouble(s);
    }
    catch (IOException ioe) { }

    Have I to write such much code to do such simple think?
    In C++ I nead only:
    double d;
    cin >> d;

    2) I need to detect is someone pres some key, without echo on console
    and without press enter after key. In C++ it's like this:

    char c = getch();

    Is in Java something like this?

    thanks in advance
     
    , Aug 2, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    > I've got 2 question:
    > 1) I'd like to read a double value from console, so I wrote:
    >
    > try{
    > char c = new char[100];
    > InputStreamReader isr = new InputStreamReader(System.in);
    > isr.read(c);
    > String s = new String(c);
    > double d = Double.parseDouble(s);
    > }
    > catch (IOException ioe) { }


    Suppressing exceptions by catching them but doing nothing about it is
    almost always a bad choice. If you are not going to do anything about
    the I/O failure at least get rid of the try-catch block, adding "throws
    IOException" to the method signature, so that it will propagate up the
    stack without being hidden.

    >
    > Have I to write such much code to do such simple think?
    > In C++ I nead only:
    > double d;
    > cin >> d;


    I would have put "throws IOException" on the method declaration and written:

    BufferedReader inReader = new BufferedReader(new
    InputStreamReader(System.in));
    String inData = inReader.readLine();
    double someMeaningfulName = Double.parseDouble(inData);

    C++ is generally a more terse language than Java. At the time C was
    designed, a lot of work was being done over relatively slow phone
    connections, so that terseness was good. Java was designed at a time of
    high bandwidth connections and IDEs with completion capabilities. It is
    less terse in its built-in features and tends to encourage meaningful
    identifiers and other non-terse programming practices.

    >
    > 2) I need to detect is someone pres some key, without echo on console
    > and without press enter after key. In C++ it's like this:
    >
    > char c = getch();
    >
    > Is in Java something like this?
    >


    Not, as far as I know, for console input. Normally you are expected to
    do that sort of thing in a GUI. There are ways of attaching a Listener
    to a GUI component to capture key presses.

    Again, at the time C was designed people did a lot of GUI-ish things on
    text consoles. In the 1980's, used Emacs as a sort of GUI environment,
    with multiple files being edited and at least one shell all in a single
    80 by 24 text display. Java was designed in a graphical display world,
    and tends to favor graphical displays for user interaction.

    Java and C++ are very different languages. I tend to get from idea to
    working, documented program faster in Java than in C++, and find it
    easier to maintain and refactor. Those things are far more valuable to
    me than terseness, so I prefer Java. YMMV.

    Patricia
     
    Patricia Shanahan, Aug 2, 2007
    #2
    1. Advertising

  3. wrote:
    > 1) I'd like to read a double value from console, so I wrote:
    >
    > try{
    > char c = new char[100];
    > InputStreamReader isr = new InputStreamReader(System.in);
    > isr.read(c);
    > String s = new String(c);
    > double d = Double.parseDouble(s);
    > }
    > catch (IOException ioe) { }
    >
    > Have I to write such much code to do such simple think?
    > In C++ I nead only:
    > double d;
    > cin >> d;


    You need to do once:

    Scanner scn = new Scanner(System.in);

    and then for each time you want to read a double:

    double d = sv.nextDouble();

    (and to catch exceptions somewhere but not necessarily with
    a try catch around every read)

    > 2) I need to detect is someone pres some key, without echo on console
    > and without press enter after key. In C++ it's like this:
    >
    > char c = getch();
    >
    > Is in Java something like this?


    Actually it is not like that in C++.

    It is like that in certain C++ compilers that are compatible with
    certain popular C compilers in the early 90's.

    getch is not part of ANSI C or ANSI C++.

    I am not aware of any way to do the same in a Java console app.

    Probably due to the fact that the feature is not possible on all
    platforms.

    Arne
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Aug 12, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dinosaur

    InputStream & InputStreamReader

    dinosaur, Sep 21, 2003, in forum: Java
    Replies:
    1
    Views:
    6,310
    Herman Timmermans
    Sep 21, 2003
  2. Robert
    Replies:
    0
    Views:
    3,068
    Robert
    Oct 5, 2003
  3. Markus Barchfeld

    InputStreamReader reads data only partially

    Markus Barchfeld, Nov 3, 2003, in forum: Java
    Replies:
    5
    Views:
    979
    Gordon Beaton
    Nov 6, 2003
  4. Rick Genter
    Replies:
    1
    Views:
    2,361
    Chris Berg
    Nov 10, 2003
  5. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    413
    Fredrik Lundh
    Aug 13, 2008
Loading...

Share This Page