4

I am wanting to print things in a neat columns using printf.

happening       0.083333    [4]
hi              0.083333    [0]
if              0.083333    [8]
important       0.083333    [7]
is              0.250000    [3, 5, 10]
it              0.166667    [6, 9]
tagged          0.083333    [11]
there           0.083333    [1]
what            0.083333    [2]

I have this code System.out.printf("%s %.6f %s \n", word, web.getFrequency(word), loc);, but it prints out this:

happening  0.083333  [ 4 ] 
hi  0.083333  [ 0 ] 
if  0.083333  [ 8 ] 
important  0.083333  [ 7 ] 
is  0.250000  [ 3 5 10 ] 
it  0.166667  [ 6 9 ] 
tagged  0.083333  [ 11 ] 
there  0.083333  [ 1 ] 
what  0.083333  [ 2 ] 

Any help would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Vayelin
  • 75
  • 1
  • 1
  • 10
  • Pad the line to a specified width: http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java – the happy mamba Nov 01 '15 at 20:02
  • Possible duplicate: http://stackoverflow.com/questions/699878/is-there-an-easy-way-to-output-two-columns-to-the-console-in-java Which suggest to use the width and precision specifiers, set to the same value (eg. System.out.printf("%-30.30s %-30.30s%n", v1, v2); ). Also see for reference: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax – Bryan Oemar Nov 01 '15 at 20:02
  • I don't know about java, but in C this would require `\t` – kebs Nov 01 '15 at 20:04
  • http://stackoverflow.com/questions/15215326/how-can-i-create-table-using-ascii-in-a-console/15215434#15215434 – Pshemo Nov 01 '15 at 20:11

4 Answers4

8

I would use this:

System.out.printf ("%-30s %1.7f %s%n", word, etc, etc2);
dmolony
  • 1,077
  • 9
  • 23
1

You can get the max length of all the word strings, and store that length in a variable, max. Use a for loop to get that max, and then create a String that has the format with concatenating the normal printf format with the max variable to be used as the width. Additionally \t puts in a tab character.

int max = 0;
for (int ii = 0; ii < numberOfStrings; ii++)
{
   max = (word.length() > max) ? word.length() : max;
}
String format = "%" + max + "s\t%.6f\t%s \n";
System.out.printf(format, word, web.getFrequency(word), loc);
lakerka
  • 38
  • 4
1
System.out.format("%-8s%-8s%-8s\n","a","b","pow(b,a)");
System.out.format("%-8d%-8d%-8d\n",1,2,1);
System.out.format("%-8d%-8d%-8d\n",2,3,8);
System.out.format("%-8d%-8d%-8d\n",3,4,81);
System.out.format("%-8d%-8d%-8d\n",4,5,1024);
System.out.format("%-8d%-8d%-8d\n",5,6,15625);

//the negative integer kinda sets it back to the number of space needed
Garen Goh
  • 9
  • 1
0

input:

happening       0.083333    [4]
hi              0.083333    [0]
if              0.083333    [8]
important       0.083333    [7]
is              0.250000    [3, 5, 10]
it              0.166667    [6, 9]
tagged          0.083333    [11]
there           0.083333    [1]
what            0.083333    [2]

code:

import java.util.Scanner;
public class MyClass { 
    private static final Scanner sc = new Scanner(System.in);
       public static void main(String args[]) {
            for(int i = 0 ; i < 9; i++){
                String str = sc.next();
                float f = sc.nextFloat();
                String str2 = sc.nextLine();
                System.out.printf("%-10s %f %s\n", str, f, str2);
             }
             sc.close();
      }   
  }

output:

happening       0.083333    [4]
hi              0.083333    [0]
if              0.083333    [8]
important       0.083333    [7]
is              0.250000    [3, 5, 10]
it              0.166667    [6, 9]
tagged          0.083333    [11]
there           0.083333    [1]
what            0.083333    [2]

use - to make the string left aligned, use field width to align them properly. In your case printf statement will be

System.out.printf("%-10s %.6f %s\n", word, web.getFrequency(word), loc);
daemonThread
  • 956
  • 1
  • 10
  • 22