Results 1 to 5 of 5
  1. #1
    inshallah is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Input a line from System.in without doing a CR/LF

    Hi Guys,

    Just joined up. Hello everyone.

    I'm using Java for the first time, and I've been trying to figure this one out for hours and it's bugging me. Can anyone help?

    I want to get some input from System.in, but when I press 'Enter' I don't want to do a CR/LF - I want the response from my program to be printed on the same line that I was typing on, not the next line.

    For example, if the input that I type in is "3 4 + .", and the response from the computer is "7 ok", then I want the "7 ok" to be printed after my input, and only then go on to the next line, like this:

    3 4 + . 7 ok
    val @ . 66 ok

    So, the newline is only started after the 'ok', not when I hit 'Enter'.

    I've tried java.util.Scanner and something along the lines of

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    Many thanks if anyone can help.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    It's the operating system that manages a standard input stream (stdin for short) per process. The stdin is line buffered (note that you can press the backspace and have some simple editing facilities on that line). If you want to read from that stream unbuffered you have to tell the operating system to stop the buffering. In unixes (and linux) it's called 'raw mode' (the buffered mode is called 'cooked mode').

    Setting the buffering style is an operating system thingy and it's beyond Java. You either have to run the 'stty' process in unix (linux) or use JNI code. There's also 'jcurses' (Google is your friend here) that can do it for you. Jcurses is Java code and has a documented API.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    inshallah is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    ok. thanks. not straight forward then.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    inshallah is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2010, 09:00 PM
  2. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 10:40 PM
  3. Multiple line user input
    By jointhelabel in forum New To Java
    Replies: 3
    Last Post: 10-26-2008, 02:41 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:48 PM
  5. Multiple Line Input Dialog Box
    By johnt in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 09:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •