Equal spacing in Enumerations?

落花浮王杯 提交于 2019-12-25 03:13:44

问题


I was working on a Java lesson and the section was on Enumerations. I have this in my Enum:

public enum tuna {
 Camaro("Orange", "1968"),
 Silverado("Red","1996"),
 Sierra("Black","2007"),
 Equinox("Silver","2011");

  private final String color;
  private final String year;


tuna(String carColor, String age){
    color = carColor;
    year = age;

 }
public String getColor(){
    return color;
 }

public String getYear(){
    return year;
 }
}

and this in my Java Class that prints it out:

for(tuna cars: tuna.values()){
    System.out.printf("%s\t\t%s\t\t%s\n", cars, cars.getColor(), cars.getYear()

Which prints out:

See how the "Red" and "1996" for the Silverado are way over on the right (because "Silverado" is longer than the other words)? Well how can I fix it so the details for long words are equally spaced as the rest?

P.S. if I shorten "Silverado" to "Silver", it's normal:


回答1:


I'd suggest not using tabs but string formatting, i.e. use System.out.format(...) and calculate the length of each format specifier by getting the length of the longest name. Then build the format string dynamically.

Example:

//that's a shortcut, you'd have to calculate the max
int maxlength = tuna.Silverado.name().length();

for( tuna t : tuna.values() ) {
  System.out.format( "%-" + maxlength + "s   %-10s %4s\n", t.name(), t.getColor(), t.getYear() );
}

Output:

Camaro      Orange     1968
Silverado   Red        1996
Sierra      Black      2007
Equinox     Silver     2011



回答2:


A tab charcter (\t) moves the cursor ahead until it reaches a position divisible by the tabulator size: if the size is e.g. 8, it will go 0, 8, 16, 24, etc. Since "Silverado" is 9 characters long, this would result in a value of 16, not 8.

You have to do your own tabbing: add spaces based on the length of the preceding string instead of using \t.

If you want the simplest solution, determine a suitable maximum length for each field and calculate your tabbing based on that, optionally cutting off strings that are longer than this.

For a dynamic solution, you would have to check the maximum length you will need based on the items you have available. This allows you to get the minimum amount of spacing required.




回答3:


Don't use tabs at all. Instead use printf better to its maximize its flexibility by using int constants with your %s fields. i.e., %10s or %-10s or whatever int constants work best for that column.




回答4:


This has nothing to do with enumerations. The only thing that matters is this line:

System.out.printf("%s\t%s\t\t%s\n", cars, cars.getColor(), cars.getYear()

What you need is string padding. You can use System.out.format methods, which allows padding in the form %5s, which means that it will pad the string to the left up to 5 spaces.

So you can compute the max length of your fields and build dynamically a format string:

    int maxCars = 0;
    int maxColor = 0;
    int maxYear = 0;

    for(tuna cars: tuna.values()) {
        maxCars = Math.max(maxCars, cars.toString().length());
        maxColor = Math.max(maxColor, cars.getColor().length());
        maxYear = Math.max(maxYear, cars.getYear().length());
    }

    String formatStr = "%"+(maxCars+5)+"s%"+(maxColor+5)+"s%"+(maxYear+5)+"s\n";

    for(tuna cars: tuna.values())
        System.out.format(formatStr, cars, cars.getColor(), cars.getYear());

The output is:

        Camaro     Orange     1968
     Silverado        Red     1996
        Sierra      Black     2007
       Equinox     Silver     2011



回答5:


This is a tab spacing thing - you'll get the same if you use tabs in this way with a text editor. For variables which differ in max/min lengths by > 4 characters (such as the first column), insert 2 tabs instead of one like this:

for(tuna cars: tuna.values()){
    if (cars.getColor().length() > 5)
        System.out.printf("%s\t%s\t\t%s\n", cars, cars.getColor(), cars.getYear())
    else
        System.out.printf("%s\t\t%s\t\t%s\n", cars, cars.getColor(), cars.getYear())

UPDATE Corrected to only output two tabs where the color isn't too long enough.



来源:https://stackoverflow.com/questions/8964973/equal-spacing-in-enumerations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!