I wrote a program which prints out the characters of the sentence (or word) wrote by the user to the console. I thought that the program will end after I gave the first input. But it didn't and kept taking inputs and printing it even after it printed the first sentence. Can you explain me why it happened so? I am new to this. Here is the program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/*
* This program prints out the characters written in the console
* line by line.
*/
public class ReaderProgram {
public static void main(String args[]) throws IOException{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do{
//reads character and stores it in c
c = (char) br.read();
//prints out c
System.out.println(c);
}while(c != -1);
//'while' checks if c is -1 (-1 means end of the stream)
}
}
Output is shown here (Input to console is show like this):
Epic
E
p
i
c
Dream
D
r
e
a
m