Take a new line with printf in java? Is it correct?

new here, but I'm in my mind. I have my program for work, but I just need help with formatting when printing. A.

if(count == 3) System.out.printf ("%-15s %15s %15s %15s %15s %n", n, " is compatible with ",dates[k],dates[k+1],dates[k+2]); 

My conclusion

 Stacey Francis is compatible with Owen Farrell Jack Clifford Joshua Watkins 

I would like my result to be (without repeating the name stacey francis or "compatible with":

 Stacey Francis is compatible with Owen Farrell Jack Clifford Joshua Watkins 

Just wondering how to do this?

Thanks,

Kirsty

+4
source share
3 answers

Yes, %n is a new line in printf. See the java.util.Formatter Documentation, in particular the conversion table, which indicates:

' n ' line separator. The result is a platform line separator

Currently, your output has only a one-line extension, and not at the points that seem to them to you. You will need to use a format, for example:

 "%-15s %15s %15s %n %15s %n %15s %n" 

(and maybe some tabs were selected for alignment).

+6
source

%n should work. But the problem is that you just used it at the end of the format string. You need to insert it in the appropriate places: -

 "%-15s %15s %15s %n %45s %n %45s" 

You can also use "\n" between your format specifiers to print a new line: -

 System.out.printf ("%-15s %15s %15s \n %45s \n %45s", n, " is compatible with ", dates[k],dates[k+1],dates[k+2]); 

In addition, I increased the length last two names from 15 to 45 to format them just below the previous names.

+2
source

You can try this.

  if(count == 3) System.out.printf ("%-15s %15s %15s %15s %15s %n", n, " is compatible with ",dates[k],dates[k+1],dates[k+2]+"\n"); 
0
source

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


All Articles