如何格式化左边的String和右边的int?
How to format String on the left and int on the right?
我正在尝试从 2 个数组中获取格式化输出,一个是国家,另一个是人口,它们应该提供如下输出:
Egypt | 92592000
France | 66991000
Japan | 126860000
Switzerland | 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]);
}
您可能正在寻找的模式是:
"%-" + maxCountryLength + "s | %" + maxPopulationLength + "d\n"
%-Xs
表示“宽度为 X
个字符,left-justified String
”
%Xd
表示“宽度为 X
个字符,right-justified 个数字”
\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]);
}
}
结果:
Egypt | 92592000
France | 66991000
Japan | 126860000
Switzerland | 8401120
使用 Java 8
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) {
if (countries.length == 0 || populations.length == 0 || countries.length != populations.length) {
return;
}
int longestCountry = Arrays.stream(countries)
.map(String::toString)
.mapToInt(String::length)
.max()
.getAsInt();
int longestPop = Arrays.stream(populations)
.mapToObj(Integer::toString)
.mapToInt(String::length)
.max()
.getAsInt();
for (int i = 0; i < countries.length; i++) {
System.out.printf("%-" + longestCountry + "s | %" + longestPop + "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]);
}
首先得到最长的国家:
for(String country: countries){ //get longest country
if (country.length() > countryLength)
countryLength = country.length();
}
其次得到最长的种群String.valueOf(i).length()
:
for(int i : populations) { //get longest number
if(String.valueOf(i).length() > populationLength)
populationLength = 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"};
Integer[] populations = {92592000, 66991000, 126860000, 8401120};
printTable(countries, populations);
}
public static void printTable(String[] countries, Integer[] 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;
}
String longestString = getLongestString( countries );
System.out.format("longest string: '%s'\n", longestString);
Integer longestNumber = getLongestNumber( populations );
System.out.format("longest Number: '%s'\n", longestNumber);
for(int i = 0; i < countries.length; i++)
System.out.format("%-" + longestString.length() + "s | %" + String.valueOf(longestNumber).length() + "d\n", countries[i], populations[i]);
}
找到字符串数组中最长的字符串
public static String getLongestString(String[] array) {
int maxLength = 0;
String longestString = null;
for (String s : array) {
if (s.length() > maxLength) {
maxLength = s.length();
longestString = s;
}
}
return longestString;
}
找出整数数组中最长的数
public static Integer getLongestNumber(Integer[] array) {
int maxLength = 0;
Integer longestNumber = null;
for (Integer i : array) {
if (String.valueOf(i).length() > maxLength) {
maxLength = String.valueOf(i).length();
longestNumber = i;
}
}
return longestNumber;
}
输出 «
longest string: 'Switzerland'
longest Number: '126860000'
Egypt | 92592000
France | 66991000
Japan | 126860000
Switzerland | 8401120
我重写了 printTable 方法,它完美地工作:
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]);
}
}
}
我正在尝试从 2 个数组中获取格式化输出,一个是国家,另一个是人口,它们应该提供如下输出:
Egypt | 92592000 France | 66991000 Japan | 126860000 Switzerland | 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]);
}
您可能正在寻找的模式是:
"%-" + maxCountryLength + "s | %" + maxPopulationLength + "d\n"
%-Xs
表示“宽度为X
个字符,left-justifiedString
”%Xd
表示“宽度为X
个字符,right-justified 个数字”\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]);
}
}
结果:
Egypt | 92592000
France | 66991000
Japan | 126860000
Switzerland | 8401120
使用 Java 8
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) {
if (countries.length == 0 || populations.length == 0 || countries.length != populations.length) {
return;
}
int longestCountry = Arrays.stream(countries)
.map(String::toString)
.mapToInt(String::length)
.max()
.getAsInt();
int longestPop = Arrays.stream(populations)
.mapToObj(Integer::toString)
.mapToInt(String::length)
.max()
.getAsInt();
for (int i = 0; i < countries.length; i++) {
System.out.printf("%-" + longestCountry + "s | %" + longestPop + "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]);
}
首先得到最长的国家:
for(String country: countries){ //get longest country
if (country.length() > countryLength)
countryLength = country.length();
}
其次得到最长的种群String.valueOf(i).length()
:
for(int i : populations) { //get longest number
if(String.valueOf(i).length() > populationLength)
populationLength = 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"};
Integer[] populations = {92592000, 66991000, 126860000, 8401120};
printTable(countries, populations);
}
public static void printTable(String[] countries, Integer[] 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;
}
String longestString = getLongestString( countries );
System.out.format("longest string: '%s'\n", longestString);
Integer longestNumber = getLongestNumber( populations );
System.out.format("longest Number: '%s'\n", longestNumber);
for(int i = 0; i < countries.length; i++)
System.out.format("%-" + longestString.length() + "s | %" + String.valueOf(longestNumber).length() + "d\n", countries[i], populations[i]);
}
找到字符串数组中最长的字符串
public static String getLongestString(String[] array) {
int maxLength = 0;
String longestString = null;
for (String s : array) {
if (s.length() > maxLength) {
maxLength = s.length();
longestString = s;
}
}
return longestString;
}
找出整数数组中最长的数
public static Integer getLongestNumber(Integer[] array) {
int maxLength = 0;
Integer longestNumber = null;
for (Integer i : array) {
if (String.valueOf(i).length() > maxLength) {
maxLength = String.valueOf(i).length();
longestNumber = i;
}
}
return longestNumber;
}
输出 «
longest string: 'Switzerland'
longest Number: '126860000'
Egypt | 92592000
France | 66991000
Japan | 126860000
Switzerland | 8401120
我重写了 printTable 方法,它完美地工作:
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]);
}
}
}