Java 如何格式化左边的字符串和右边的int?

Java 如何格式化左边的字符串和右边的int?,java,format,formatter,Java,Format,Formatter,我试图从2个数组中获得格式化输出,一个是国家,另一个是人口,应提供如下输出: Egypt | 92592000 France | 66991000 Japan | 126860000 Switzerland | 8401120 埃及| 92592000 法国| 66991000 日本124860000 瑞士| 8401120 我收到的唯一提示是,我应该计算每列所需的最大宽度,然后使用它来对齐值。 这就是我到目前为止所想到的,但我一直坚持在格式化时输出任何

我试图从2个数组中获得格式化输出,一个是国家,另一个是人口,应提供如下输出:

Egypt | 92592000 France | 66991000 Japan | 126860000 Switzerland | 8401120 埃及| 92592000 法国| 66991000 日本124860000 瑞士| 8401120 我收到的唯一提示是,我应该计算每列所需的最大宽度,然后使用它来对齐值。 这就是我到目前为止所想到的,但我一直坚持在格式化时输出任何内容

public static void main (String[] args) throws java.lang.Exception
{
    String [] countries = {"Egypt", "France", "Japan", "Switzerland"};
    int[] populations = {92592000, 66991000, 126860000, 8401120};
    printTable(countries, populations);
}

public static void printTable(String[] countries, int[] populations){
    int longestInput = 0;
    for(int i = 0; i < countries.length; i++){
        int countLength = countries[i].length();
        int popLength = String.valueOf(populations[i]).length();
        if(countLength + popLength > longestInput)
            longestInput = countLength + popLength;
    }

    for(int i = 0; i < countries.length; i++)
    System.out.format("%-10",countries[i] + " | " + populations[i]);
}
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception
{
字符串[]国家={“埃及”、“法国”、“日本”、“瑞士”};
int[]人口={92592000、66991000、126860000、8401120};
打印表(国家、人口);
}
公共静态void打印表(字符串[]国家,整数[]人口){
int longestInput=0;
对于(int i=0;ilongestInput)
longestInput=countLength+popLength;
}
对于(int i=0;i
您可能正在寻找的模式是:

"%-" + maxCountryLength + "s | %" + maxPopulationLength + "d\n"
  • %-Xs
    表示“
    X
    字符宽度,左对齐的
    字符串
  • %Xd
    表示“
    X
    字符宽度,右对齐的数字”
  • \n
    表示它占用一行
该方法可能如下所示:

public static void printTable(String[] countries, int[] populations) {
    int defaultLength = 10;
    int maxCountryLength = stream(countries).mapToInt(String::length).max().orElse(defaultLength);
    int maxPopulationLength = stream(populations).mapToObj(Integer::toString).mapToInt(String::length).max().orElse(defaultLength);

    for (int i = 0; i < countries.length; i++) {
        System.out.format("%-" + maxCountryLength + "s | %" + maxPopulationLength + "d\n", countries[i], populations[i]);
    }
}

您可能正在寻找的模式是:

"%-" + maxCountryLength + "s | %" + maxPopulationLength + "d\n"
  • %-Xs
    表示“
    X
    字符宽度,左对齐的
    字符串
  • %Xd
    表示“
    X
    字符宽度,右对齐的数字”
  • \n
    表示它占用一行
该方法可能如下所示:

public static void printTable(String[] countries, int[] populations) {
    int defaultLength = 10;
    int maxCountryLength = stream(countries).mapToInt(String::length).max().orElse(defaultLength);
    int maxPopulationLength = stream(populations).mapToObj(Integer::toString).mapToInt(String::length).max().orElse(defaultLength);

    for (int i = 0; i < countries.length; i++) {
        System.out.format("%-" + maxCountryLength + "s | %" + maxPopulationLength + "d\n", countries[i], populations[i]);
    }
}
使用Java8
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{
字符串[]国家={“埃及”、“法国”、“日本”、“瑞士”};
int[]人口={92592000、66991000、126860000、8401120};
打印表(国家、人口);
}
公共静态void打印表(字符串[]国家,整数[]人口){
if(countries.length==0 | | populations.length==0 | | | countries.length!=populations.length){
回来
}
int longestCountry=Arrays.stream(国家)
.map(字符串::toString)
.mapToInt(字符串::长度)
.max()
.getAsInt();
int longestPop=Arrays.stream(总体)
.mapToObj(整数::toString)
.mapToInt(字符串::长度)
.max()
.getAsInt();
对于(int i=0;i
诀窍是使用streams获得长度,并使用负格式字符串填充字符串的右侧。

使用Java 8
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{
字符串[]国家={“埃及”、“法国”、“日本”、“瑞士”};
int[]人口={92592000、66991000、126860000、8401120};
打印表(国家、人口);
}
公共静态void打印表(字符串[]国家,整数[]人口){
if(countries.length==0 | | populations.length==0 | | | countries.length!=populations.length){
回来
}
int longestCountry=Arrays.stream(国家)
.map(字符串::toString)
.mapToInt(字符串::长度)
.max()
.getAsInt();
int longestPop=Arrays.stream(总体)
.mapToObj(整数::toString)
.mapToInt(字符串::长度)
.max()
.getAsInt();
对于(int i=0;i

诀窍是使用streams获得长度,并使用负格式字符串填充字符串的右侧。

这是一个小示例:

public static void main (String[] args) throws java.lang.Exception
  {
    String [] countries = {"Egypt", "France", "Japan", "Switzerland"};
    int[] populations = {92592000, 66991000, 126860000, 8401120};
    printTable(countries, populations);
  }

  public static void printTable(String[] countries, int[] populations){
    int countryLength = 0;
    long populationLength = 0;

    for(String country: countries){ //get longest country
      if (country.length() > countryLength)
        countryLength = country.length();
    }
    for(int i : populations) { //get longest number
      if(String.valueOf(i).length() > populationLength)
        populationLength = String.valueOf(i).length();
    }

    for(int i = 0; i < countries.length; i++) // print it out
      System.out.format("%-"+ (countryLength+1) +"s|%" + (populationLength+1) +"d\n",countries[i], populations[i]);
  }
其次,使用
String.valueOf(i).length()获得最长的填充:

最后用
System.out.format
打印出来:

System.out.format("%-"+ (countryLength+1) +"s|%" + (populationLength+1) +"d\n",countries[i], populations[i]);

这是一个小例子,你可以这样做:

public static void main (String[] args) throws java.lang.Exception
  {
    String [] countries = {"Egypt", "France", "Japan", "Switzerland"};
    int[] populations = {92592000, 66991000, 126860000, 8401120};
    printTable(countries, populations);
  }

  public static void printTable(String[] countries, int[] populations){
    int countryLength = 0;
    long populationLength = 0;

    for(String country: countries){ //get longest country
      if (country.length() > countryLength)
        countryLength = country.length();
    }
    for(int i : populations) { //get longest number
      if(String.valueOf(i).length() > populationLength)
        populationLength = String.valueOf(i).length();
    }

    for(int i = 0; i < countries.length; i++) // print it out
      System.out.format("%-"+ (countryLength+1) +"s|%" + (populationLength+1) +"d\n",countries[i], populations[i]);
  }
其次,使用
String.valueOf(i).length()获得最长的填充:

最后用
System.out.format
打印出来:

System.out.format("%-"+ (countryLength+1) +"s|%" + (populationLength+1) +"d\n",countries[i], populations[i]);
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{
字符串[]国家={“埃及”、“法国”、“日本”、“瑞士”};
整数[]总体={92592000、66991000、126860000、8401120};
打印表(国家、人口);
}
公共静态void打印表(字符串[]国家,整数[]人口){
int longestInput=0;
对于(int i=0;ilongestInput)
longestInput=countLength+popLength;
}
字符串longestString=getLongestString(国家);
System.out.format(“最长字符串:'%s'\n',最长字符串”);
整数longestNumber=getLongestNumber(总体);
System.out.format(“最长的数字:'%s'\n',最长的数字”);
对于(int i=0;i
在字符串数组中查找最长字符串的步骤

公共静态字符串getLongestString(字符串[]数组){
int maxLength=0;
字符串longestString=null;
for(字符串s:数组){
如果(s.length()>maxLength){
maxLength=s.length()
public static void printTable(String[] countries, int[] populations)
{
    if(countries.length != 0)
    {

        int longestNameInput = countries[0].length();
        int longestPopInput = String.valueOf(populations[0]).length();

        for(int i = 0; i < countries.length; i++)
        {
            int countLength = countries[i].length();
            int popLength = String.valueOf(populations[i]).length();

            if(countLength > longestNameInput)
                longestNameInput = countLength;

            if(popLength > longestPopInput)
                longestPopInput = popLength;
        }

        for(int i = 0; i < countries.length; i++)
        {
            System.out.print(countries[i]);
            for(int j = 0; j < (longestNameInput - countries[i].length()); j++)
                System.out.print(" ");
            System.out.print(" | ");

            for(int k = 0; k < (longestPopInput - String.valueOf(populations[i]).length()); k++)
                System.out.print(" ");
            System.out.println(populations[i]);
        }

    }
}