How to use the Java formatter
- Paste your code into the Input box. The sample is a
Greeterclass with no indentation at all. - Pick the Indent: 4 spaces (default), 2 spaces or Tab.
- Choose Opening braces: Keep as written, Same line (K&R / Java style) or Own line (Allman / Microsoft style).
- Leave Normalise spacing ticked for
if (,) {and} else, and set Max blank lines in a row (default 1). - Copy the Formatted Java from the result.
What it does and when to use it
Java pasted from a PDF, a chat message, an old forum post or a student’s editor often arrives flush-left or with a mix of tabs and spaces. Reading nested loops in that state is slow. This tool restores the nesting so each block sits one level inside its parent, and can switch brace placement in one go. The yellow note under every result is a reminder that it is not a full Java formatter.
Working with configuration too? The Java properties formatter cleans .properties files and the XML formatter tidies pom.xml. For the same job in other brace languages, see the C# formatter and C/C++ formatter.
How it works
- Mask. Each line is scanned, and every character inside a string, char literal, comment or
"""text block is hidden from the next steps. - Braces. With Same line, a
{alone on a line joins the line above, andelse,catchandfinallymove up next to}. With Own line, a trailing{moves to its own line. - Indent. A stack of open
{,(and[gives each line its level.caseanddefaultlabels sit one level insideswitch, their statements one more, and lines starting with.method()get one extra level. - Space. The few spacing rules above are applied to code only.
It all runs in your browser; nothing is uploaded.
Worked examples
These are the tool’s own outputs with default settings.
The sample class. The flush-left code becomes properly nested, starting:
public class Greeter {
public static void main(String[] args) {
for (int i=0;i<3;i++) {
The { that was on its own line under public class Greeter has moved up.
try, catch and finally. try{ … }catch(IOException e){ … } and finally{ on a separate line give:
try {
run();
} catch (IOException e) {
log(e);
} finally {
close();
}
A text block and a stream chain. The three lines inside """ keep their own indentation, while .filter(x->x>0) and .map(x->x*2) are indented one level under list.stream(). The note adds “3 line(s) inside multi-line strings or templates were left exactly as they were”.
Limits and tips
- No line wrapping and no blank-line rules between methods.
- Annotations and generics are left as written, so
@Override public voidon one line stays on one line. - Run your project’s formatter (Spotless, IntelliJ) before committing; use this for quick clean-ups.
Frequently asked questions
Is this the same as google-java-format or the IntelliJ formatter?
Why is i=0;i<3 still written without spaces?
Will it break my strings or comments?
Which brace style should I pick?
Does it check that my code compiles?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.