简体   繁体   中英

How can I print out in columns in java

This is where I am printing out and I need it to print in columns.aLeaderboard is an array list with a custom class.it contains several different ints

System.out.println("Position     Team                Games Played    Home Wins   Home Draws   Home Losses  Home Goals For   Home Goals Against   Away Wins   Away Draws  Away Losses  Away Goals For    Away Goals Against   Goal Difference    Total Points");
  for(int counter = 0;counter<teamName.size();counter++)
  {
  System.out.print((counter + 1) + "            " +  teamName.get(counter) + "     " + (aLeaderboard.get(counter)).getGamesPlayed() + "                  " + (aLeaderboard.get(counter)).getHomeWins() + "              " +  (aLeaderboard.get(counter)).getHomeDraws() + "           ");
  System.out.print((aLeaderboard.get(counter)).getHomeLosses() + "        " + (aLeaderboard.get(counter)).getAwayWins() + "      " + (aLeaderboard.get(counter)).getAwayWins() + "       " + (aLeaderboard.get(counter)).getAwayDraws() + "        ");
  System.out.print((aLeaderboard.get(counter)).getHomeGoalsFor() + "         " + (aLeaderboard.get(counter)).getHomeGoalsAgainst() + "          " + (aLeaderboard.get(counter)).getAwayLosses() + "        " + (aLeaderboard.get(counter)).getGamesPlayed() + "     ");
  System.out.print((aLeaderboard.get(counter)).getAwayGoalsFor() + "         " + (aLeaderboard.get(counter)).getAwayGoalsAgainst() + "          " + (aLeaderboard.get(counter)).getGoalsDifference() + "          " + (aLeaderboard.get(counter)).getTotalPoints());
  System.out.println();
  }

I would use System.out.printf(...) and use a template String to help be sure that all columns line up. Then you could print things out easily in a for loop.

For example:

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class Foo4 {
   public static void main(String[] args) {
      List<Bar4> bar4List = new ArrayList<>();
      bar4List.add(new Bar4("Donald", 3, "A", 22.42));
      bar4List.add(new Bar4("Duck", 100, "B", Math.PI));
      bar4List.add(new Bar4("Herman", 20, "C", Math.sqrt(20)));

      String titleTemplate = "%-10s %6s %6s %9s%n";
      String template = "%-10s %6d %6s %9s%n";

      System.out.printf(titleTemplate, "Name", "Value", "Grade", "Cost");
      for (Bar4 bar4 : bar4List) {
         System.out.printf(template, bar4.getName(), 
               bar4.getValue(), bar4.getGrade(), bar4.getCostString());
      }

   }
}

class Bar4 {
   private String name;
   private int value;
   private String grade;
   private double cost;
   private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

   public Bar4(String name, int value, String grade, double cost) {
      this.name = name;
      this.value = value;
      this.grade = grade;
      this.cost = cost;
   }

   public String getName() {
      return name;
   }

   public int getValue() {
      return value;
   }

   public String getGrade() {
      return grade;
   }

   public double getCost() {
      return cost;
   }

   public String getCostString() {
      return currencyFormat.format(cost);
   }

}

Which would return:

Name        Value  Grade      Cost
Donald          3      A    $22.42
Duck          100      B     $3.14
Herman         20      C     $4.47

For more details on the user of the String format specifiers (ie, the %6d and %6s above), please look at the Formatter API .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM