Reading data from the console to exit
I have an program which keeps running all the time and when ever i type "EXIT" in the command prompt, the java application should stop running.. Can anybody help me out in this
I have the sample code likethis
class Sample{
publicstaticvoid main( String args[])
{
while(true)
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String stop=br.readLine();
if(stop.equals("x"))
break;
// or else stop the application directly ..
else
{
//do some task..
}
}
}
This program shouldnot stop running unless i give exit.
The looping shouldnot wait untill i press enter every time.
If i run the program now, each and every time we have to give ENTER to continue our task .. Is there any other way to do it so.
> A buffered reader is supposed to be able to read a
> line.
>
> Suppose I started to type in the letters EXIT. At
> this this moment how can you know I am going to type
> further letters or the line is ready, apart from my
> having typed return?
Instead of the word EXIT , let me have the letter X , so now when i give the letter X, then the loop should break.
Assume I have typed X but I am still pondering wether to delete it back and replace with something differerent.
Okay, you can have a terminal in line-editing mode enabling you to delete back etc. or have the reading applaication have any caharacter immediately as the user hit a key. Unfortunately Java does not support the latter. If feasible and applicable, GUI can be used instead.
Cf also:
http://sourceforge.net/projects/javacurses/