# Stack Overflow won't let me post code, I don't have proper "formatting"?

I was trying to answer a coding question in Java. I had some code and used <pre><code>, put my code in, then typed </pre></code>. I then came across this error when trying to post:

Oops! Your edit couldn't be submitted because:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

It is properly formatted (at least by the preview it is), so how can I fix this? I'm new to Stack Overflow formatting.

• possible duplicate of How do I format my code blocks? Jul 13 '12 at 6:50
• So why not just click those little {} in the format toolbar, which do nothing more than put 4 spaces in front of each line, instead of those weird XML tag thingies? Jul 13 '12 at 6:50
• @UristMcBobby What are you talking about? Jul 13 '12 at 6:52
• @ChristianRau Creating the HTML yourself is a supported way of formatting code. The validator should accept that. Jul 13 '12 at 6:54
• @balpha: What? Since when?! I always thought that it was "yak, yeah, you can do that too" type, and not something which should be encouraged (as with all HTML markup)...mea culpa big time... Jul 13 '12 at 6:58
• @UristMcBobby Well, for one thing, your possible dupe lists this as a valid alternative :) Yes, you're correct we'd rather have people use Markdown, but that doesn't mean we should (as happened in this case; the code was formatted just fine) prohibit the supported use of <pre><code>ing yourself, I think. But even if we eventually decline this, it should still be explicit. Jul 13 '12 at 7:00
• If you really did it like you described here, your closing tags were switched. Jul 13 '12 at 7:08
• @balpha: Yes, that's what I meant, I can remember reading that post some time back, but could not remember that the pre-code-stuff was in there, and also not that it was encouraged (in the sense of "You can do this too"). Reading this question, I just thought you guys killed it off. Jul 13 '12 at 7:56
• I cannot answer for some reason... :/ However, it seems like you may have added some pseudo-code as text which SO thinks is actual code and hasn't been formatted. For example, I had psuedo-code in the form of a list like: this_list = [] that_list = [] And so it wasn't happy that wasn't given as code. Oct 2 '19 at 10:48

Markdown automatically formats such lines (those that start with four spaces) as code. When pasting code, you can have it automatically formatted for you by clicking on the button. Alternatively, you can put the four spaces in front of the code lines yourself. As a result, your code looks like this:

    class Something{
int i;
public void DoSomething(){
//some code here
}
}


You also have to make sure that you leave at least one blank line between your statements/paragraphs and your code to ensure the code gets detected and formatted properly.

The alternative is to wrap the code in <pre><code> tags. Formatting of raw HTML can be sometimes terse so you'll have to make sure you close the tags in reverse to the order you opened them. In other words, open and close tags like so:

<pre><code>
class Something{
int i;
public void DoSomething(){
//some code here
}
}
</code></pre>


If you interchange the position of the closing tags, it is possible that markdown will not format your code properly or sometimes it will not be formatted as code at all!

• With <pre><code> one does not need the four spaces. Also, one would need to manually encode HTML entities, such as &lt; or &#60; for <, and &amp; or &#38; for &. Jul 13 '12 at 11:02
• @Arjan: Sorry for the oversight. I've updated the answer now. Jul 13 '12 at 13:56
• Option 2 is exactly how my code block is formatted (so I can highlight changes in the code) and I'm still getting the error. (And don't get me started on why a heuristic that can fail blocking submission. I never would have guessed that message meaning I couldn't submit. I spent a lot of time on my answer to have it all wasted.) May 2 '17 at 22:56
• @trlkly: If you are absolutely sure that that's how you've formatted your response, then I cannot determine what may be causing the error. You probably might have to get in touch with the site's moderators to see if there's anything peculiar about your input. May 8 '17 at 5:56

If you have any headings in your post, this could also be the case the warning is (wrongly) shown.

As proposed in this answer, you should make sure there is an empty line after each heading.

Excuse me for repeating message you've just cited, but didn't system tell you plain and clear:

Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut.

If you do just that, you'll get your code in and it will even get automatically highlighted.

• I did, it didn't work. But Alex said that I need to have the tags in reverse, I'll try that. Jul 13 '12 at 17:55