New to Java - String format = "|% -" + maxW [j] + "s"; - What does this line do?

I am on the Nob in Java, and I came across the code below and could not understand its function. maxw [] is an array of type int. row [] is an array of type String.

String format = " | %-"+maxW[i]+"s"; System.out.printf(format,row[i]); 

My questions: For this statement: System.out.printf (format, row [i]); only '|' and after that, the value of the string [i] is printed, so why "% - (value maxW [i]) and" s "are not printed?

+4
source share
2 answers

Java format specifiers are described here .

This code dynamically creates a format specifier that will left-justify the row (row [i]) with the minimum width specified by maxW [i].

Format specifiers match the pattern in the first line below. Below me, the code is aligned, showing where it fits into the template.

  %[argument_index$][flags] [width] [.precision]conversion " | % - "+maxW[i] +"s"; 
+5
source

The format method assumes that String declares the format, as indicated in the documentation. It processes% x or the like in a special way, replacing it with the first, second argument etc.

If you want to know more, I suggest you read the Javadoc for this method.

0
source

Source: https://habr.com/ru/post/1333219/


All Articles