Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing with "\t" (tabs) does not result in aligned columns

I have a very weird problem. After writing this:

for (File f : currentFile.listFiles()) {                 if  (f.isDirectory()){         System.out.println(f.getName()+"\t"+"Dir\t"+Command.getpremission(f)+"\t"+f.getTotalSpace());     }     else{         System.out.println(f.getName()+"\t"+"File\t"+Command.getpremission(f)+"\t"+f.getTotalSpace());     } 

I see this printed:

see.txt File    rw  267642728448 see1.txt    File    rw  267642728456 see2.txt    File    rw  267642728448 

Why is there a problem with the tabs?

like image 899
Unknown user Avatar asked May 14 '11 08:05

Unknown user


People also ask

How do you print tabs in statements?

The Escape sequence for tab, \t can be used inside a print statement, as given in the code below. The String to be formatted is placed inside the double quotes. The Escape Sequence for tab \t is placed between the words inside the "" . As we can see in the output, it inserts a tab between the two words.

How do you print tab space in Python?

The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t' . To see the tab spaced character in the REPL wrap any variable containing a tab character in the built-in print() function.

How do I print a tab in printf?

When you are printing a tab character to the standard output using printf in C, it outputs some space which is apparently 4 characters in length. printf("\t");

How do you give a tab space in Java?

"\t" format specifier is used in println() function to leave tab space in the console.


1 Answers

Building on this question, I use the following code to indent my messages:

String prefix1 = "short text:"; String prefix2 = "looooooooooooooong text:"; String msg = "indented"; /* * The second string begins after 40 characters. The dash means that the * first string is left-justified. */ String format = "%-40s%s%n"; System.out.printf(format, prefix1, msg); System.out.printf(format, prefix2, msg); 

This is the output:

 short text:                             indented looooooooooooooong text:                indented 

This is documented in section "Flag characters" in man 3 printf.

like image 103
Matthias Braun Avatar answered Sep 25 '22 06:09

Matthias Braun