What's Up with Java's "%N" in Printf

What's up with Java's %n in printf?

From a quick google:

There is also one specifier that doesn't correspond to an argument. It is "%n" which outputs a line break. A "\n" can also be used in some cases, but since "%n" always outputs the correct platform-specific line separator, it is portable across platforms whereas"\n" is not.

Please refer
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Original source

Using %n or \n in Java to format String

Well, as far as i know the difference is that %n will always output the correct line break depending on the platform while \n won't.

Java: Literal percent sign in printf statement

The percent sign is escaped using a percent sign:

System.out.printf("%s\t%s\t%1.2f%%\t%1.2f%%\n",ID,pattern,support,confidence);

The complete syntax can be accessed in java docs. This particular information is in the section Conversions of the first link.

The reason the compiler is generating an error is that only a limited amount of characters may follow a backslash. % is not a valid character.

Double % formatting question for printf in Java

%d is for integers use %f instead, it works for both float and double types:

double d = 1.2;
float f = 1.2f;
System.out.printf("%f %f",d,f); // prints 1.200000 1.200000

Why am I getting a runtime error when using printf?

Try this:

System.out.printf("%5d$, %9.2f, %5.2f, %29s\n\n", ...);

Which will print:

  • An integer padded to at least 5 spaces for the first argument
  • A float padded at least 9 spaces before the radix and has 2 digits after for the second argument
  • A float padded at least 5 spaces before the radix and has 2 digits after for the third argument
  • A string padded to at least 29 characters for the forth argument

What does %s and %d mean in printf in the C language?

The printf() family of functions uses % character as a placeholder. When a % is encountered, printf reads the characters following the % to determine what to do:

%s - Take the next argument and print it as a string
%d - Take the next argument and print it as an int

See this Wikipedia article for a nice picture: printf format string

The \n at the end of the string is for a newline/carriage-return character.

System.out.printf(%-15s%03d%n, s1, x) How to interpret it

Basically every %... is gonna be replaced by one of the arguments of printf. What is after the % sign is a format specifier.

In %-15s:

  • - means: left-justified
  • 15 means: if the result is less than 15 characters long, add spaces until it is 15 characters long
  • s means: convert the parameter into a string with toString and use the result

In %03d:

  • 0 means: pad with 0s instead of spaces
  • 3 means: make it at least 3 characters long
  • d means: the argument will be an integer number, format it as a base-10 number.

%n is the same as \n on *NIX or \r\n on Windows.

You will get more info here: https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

EDIT based on remarks by AxelH and Andy Turner

I'm getting an error with System.out.printf()

The percent sign for (13%) is being interpreted as a placeholder. It's looking for characters after the % for details about how to format a variable there, and ) isn't valid.

You meant to have a literal % in the output, not interpreted as a placeholder. It must escaped with another % sign. Try:

System.out.printf(
"\n\nThe total bill for your group is $%.2f before taxes. The HST (13%%) will be $%.2f.",
totalBill, amountOfTax);

See the Formatter javadocs for details:

The format specifiers which do not correspond to arguments have the following syntax:

%[flags][width]conversion

And

'%' percent The result is a literal '%' ('\u0025')

Difference between %n and \n for printing a new line in Java

%n is portable between various platforms, the value emitted from %n will suit the underlying platform, whereas value emitted by \n is same for all the platforms.

\n is the correct newline character for Unix-based systems, other systems may use different characters to represent the end of a line.

Windows system use \r\n, and early MacOS systems used \r.



Related Topics



Leave a reply



Submit