Reputation: 3
I am taking in a string from a website that looks along the lines of <HTML CODE HERE>Text I want to get
and remove the brackets and the text within them, however, my end result is always null.
What I am trying is,
try {
String desc = null;
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
String line = null;
boolean codeBlock;
codeBlock = false;
line = "<HTMLCODEHERE>Text I want to get";
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! STARTING DESC: " + line);
while((line = r.readLine()) != null) {
if((line = r.readLine()) == "<") {
codeBlock = true;
}
if((line = r.readLine()) == ">") {
codeBlock = false;
}
if(!codeBlock) {
sb.append(line);
desc = sb.toString();
}
}
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ENDING DESC: " + desc);
holder.txtContent.setText(desc);
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 0
Views: 1113
Reputation: 66
Have a look at the Java API for BufferedReader, namely readline:
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()
Therefore your code here:
if((line = r.readLine()) == "<") {
codeBlock = true;
}
if((line = r.readLine()) == ">") {
codeBlock = false;
}
Will never be true. Those calls also take you away from your current line of analysis.
If I understand your question correctly, you want all text in between any HTML tag? You could mess around with libraries like jsoup
or go for a simpler implementation:
String parse = "<HTMLCODE>My favourite pasta is spaghetti, followed by ravioli</HTMLCODE>";
final char TAG_START = '<';
final char TAG_END = '>';
StringBuilder sb = new StringBuilder();
char[] parseChars = parse.toCharArray();
boolean inTag = true;
for (int i = 0; i< parseChars.length; i++) {
if (parseChars[i] == TAG_START) {
inTag = true;
continue;
}
else if (parseChars[i] == TAG_END) {
inTag = false;
continue;
}
if (!inTag) {
sb.append(parseChars[i]);
}
}
System.out.println(sb.toString());
Upvotes: 1