Use Portable Newline

Properties

Property Value
First seen in jSparrow version 1.0.0
Minimum Java version 5
Remediation cost 1 min
Links FindBugs

Description

This rule replaces any occurrences of '\n' with '%n'.
For example 'String.format("\n\n")' is replaced by 'String.format("%n%n")'.

Benefits

The benefit of this approach is that '%n' is portable across platforms.

Requirement & Tags

Requirements

Java 5

Code Changes

Pre

public class TestStringFormatLineSeparatorRule {
    public String testStringFormatLineSeparator01() {
        return String.format("\n\n");
    }
    public String testStringFormatLineSeparator02() {
        return String.format(Locale.GERMAN, "\n\n");
    }
    public String testStringFormatLineSeparator03() {
        return String.format("\r\n\r\n");
    }
    public String testStringFormatLineSeparator04() {
        return String.format(Locale.GERMAN, "\r\n\r\n%n");
    }
}

Post


public class TestStringFormatLineSeparatorRule {
    public String testStringFormatLineSeparator01() {
        return String.format("%n%n");
    }
    public String testStringFormatLineSeparator02() {
        return String.format(Locale.GERMAN, "%n%n");
    }
    public String testStringFormatLineSeparator03() {
        return String.format("%n%n");
    }
    public String testStringFormatLineSeparator04() {
        return String.format(Locale.GERMAN, "%n%n%n");
    }
}