Code :
import java.io.*;
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
String phrase = "Hello World #This_Is_Comment# #This is not comment#";
byte[] bytes = phrase.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
BufferedInputStream bin = new BufferedInputStream(in);
int character;
boolean com = false;
while ((character = bin.read()) != -1) {
switch(character) {
case '#' :
if (com) com = false;
else {
com = true;
bin.mark(1000);
}
break;
case ' ' :
if (com) {
com = false;
System.out.print('#');
bin.reset();
}
else {
System.out.print((char)character);
}
break;
default : if (!com) System.out.print((char)character);
}
}
in.close(); bin.close();
}
}
What does this code do?
This code reads the String and the bufferStream removes/hides comments. Here comments are denoted by #this_is_comment# and comments with ' ' (space) is not considered comment ex : #This is not comment#.
Question:
Whenever ' ' (space) is encountered and com (boolean value, when true does not read the stream) is true it reverts the stream to the marked position, my doubt is that if it reverts, wouldn't the # be encountered again and com will be set to false hence considering it comment.
case '#' :
if (com) com = false;
else {
com = true;
bin.mark(1000);
}
but this is not the case the output is correct.
If possible please edit the question to make it more understandable.