Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can see that your intent is to remove stuff that looks like <code>&lt;xxx&gt;</code> and <code>&amp;xxx;</code>. You're using the variable <code>b</code> to remember whether you're currently skipping stuff or not.</p> <p>Did you notice that your algorithm will skip things of the form <code>&lt;xxx;</code> and <code>&amp;xxx&gt;</code>? Namely, <code>&amp;</code> or <code>&lt;</code> will cause skipping to begin, and <code>&gt;</code> or <code>;</code> will cause skipping to end, but you don't have to match <code>&lt;</code> with <code>&gt;</code>, or <code>&amp;</code> with <code>;</code>. So how about implementing code to remember which character started the skip?</p> <p>A further complication, though, is that <code>&amp;xxx;</code> stuff can be embedded in <code>&lt;xxx&gt;</code> stuff, like this: <code>&lt;p title="&amp;amp;"&gt;</code></p> <p>Incidentally, <code>temp += str.charAt(i);</code> will make your program very slow when the string is long. Look at using <code>StringBuilder</code> instead.</p> <hr> <p>Here is some code that should solve your problem or nearly so:</p> <pre><code>import java.util.Stack; public String getFilteredPageContents() { String str = getUnfilteredPageContents(); StringBuilder() temp = new StringBuilder(); // The closing character for each thing that we're inside Stack&lt;Character&gt; expectedClosing = new Stack&lt;Character&gt;(); for(int i = 0; i&lt;str.length(); i++) { char c = str.charAt(i); if(c == '&lt;') expectedClosing.push('&gt;'); else if(c == '&amp;') expectedClosing.push(';'); // Is the current character going to close something? else if(!expectedClosing.empty() &amp;&amp; c == expectedClosing.peek()) expectedClosing.pop(); else { // Only add to output if not currently inside something if(expectedClosing.empty()) temp.append(c); } } return temp.toString(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload