-1

I am executing a simple java application in eclipse.

Sample code:

 BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
    String line;
    while((line = input.readLine()) != null && line.length() != 0) 
    {
    System.out.println("------"+line);
    }

In the above code the readline() method hangs when reading the last line of my input. I have gone through some threads and understood that it waits for the end of line. I don't want to give any '\n' or '\r' at the end of my input in the console. so how to handle this in the code.

Kazekage Gaara
  • 14,516
  • 13
  • 53
  • 104
sri
  • 1
  • 5
    Please clarify the problem all seems to be fine when testing. What is the expected output vs the output you got? To exit the loop I just hit enter without typing anything – David Kroukamp Aug 19 '12 at 16:21
  • 5
    Typically you send an EOF with `Ctrl-D`. – Matt Ball Aug 19 '12 at 16:24
  • 1
    http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it – dacwe Aug 19 '12 at 16:43

1 Answers1

0

How is the code to know when you are not going to type anything else unless you press enter?

There is no solution to this issue. You need to re-think your use-case.

Dan
  • 1,002
  • 5
  • 12