PORT.LNSP
Do not hardcode `\n' or `\r' as a line separator
Description
This rule flags code where `\n' or `\r' is hardcoded as a line separator.
Different systems use different characters or sequences of characters as line separators. Therefore, hardcoding `\n', `\r', or `\r\n' damages Java code's portability.
Example
package PORT;
public class LNSP {
public printHeader (String name, String id) {
System.out.println("HEADER \n" +"Name: name \n" +"ID: id
\n");
}
}
Repair
Use the `println()' method of PrintStream or PrintWriter; this automatically terminates a line with the line separator appropriate for the platform. Or, use the value of System.getProperty(`line.separator')
package PORT;
public class LNSP {
public printHeader (String name, String id) {
System.out.println("HEADER ");
System.out.println("Name: " +name);
System.out.println("ID: " +id);
}
}
Reference
Flanagan, David. Java in a Nutshell. O'Reilly, 1999, pp. 191-192.
|