Java formatter — re-indent Java code and fix brace style

Paste Java code and this tool rebuilds its indentation from the braces, applies K&R or Allman brace placement and adds basic spacing, while leaving strings, comments and text blocks untouched.

Live demo · real result from this tool
Indent

Formatted Java

public class Greeter {    public static void main(String[] args) {        for (int i=0;i<3;i++) {            if (i%2==0) {                System.out.println("Even: "+i);            } else {                System.out.println("Odd: "+i);            }        }    }} 

Indentation and spacing were normalised by brace structure — this is not a full Java formatter, so line wrapping, blank-line rules and token spacing beyond the basics are left as written.

Processed on your device — nothing is uploaded.

How to use the Java formatter

  1. Paste your code into the Input box. The sample is a Greeter class with no indentation at all.
  2. Pick the Indent: 4 spaces (default), 2 spaces or Tab.
  3. Choose Opening braces: Keep as written, Same line (K&R / Java style) or Own line (Allman / Microsoft style).
  4. Leave Normalise spacing ticked for if (, ) { and } else, and set Max blank lines in a row (default 1).
  5. 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

  1. Mask. Each line is scanned, and every character inside a string, char literal, comment or """ text block is hidden from the next steps.
  2. Braces. With Same line, a { alone on a line joins the line above, and else, catch and finally move up next to }. With Own line, a trailing { moves to its own line.
  3. Indent. A stack of open {, ( and [ gives each line its level. case and default labels sit one level inside switch, their statements one more, and lines starting with .method() get one extra level.
  4. 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 void on 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?
No. Those parse Java fully and also wrap long lines and space every operator. This tool is a lighter re-indenter that works from braces, brackets and a few keywords. It is good for rescuing badly indented code, not for enforcing a full style guide.
Why is i=0;i<3 still written without spaces?
Spacing around operators is not changed. Normalise spacing only adds a space after if, for, while, switch and catch, before an opening brace, around else, catch and finally, and after commas.
Will it break my strings or comments?
No. A scanner marks string literals, char literals, // and /* */ comments and """ text blocks first, and their contents are never re-spaced. Lines inside a text block keep their exact indentation, and the result tells you how many there were.
Which brace style should I pick?
Most Java code, including the Oracle conventions and Google's style guide, puts the opening brace on the same line (K&R), which is the default here. Choose Own line (Allman) only if your team uses it, or Keep as written to change indentation only.
Does it check that my code compiles?
No. It does not report syntax errors. Missing braces will produce odd indentation, which can actually help you spot where a block was not closed.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close