Java 使用printf会给我未对齐的列
我试图让我的程序以整洁的格式打印数据,数据与列对齐。我尝试在类的toString方法中使用String.format,但根本没有对齐。我应该如何改变它Java 使用printf会给我未对齐的列,java,alignment,printf,Java,Alignment,Printf,我试图让我的程序以整洁的格式打印数据,数据与列对齐。我尝试在类的toString方法中使用String.format,但根本没有对齐。我应该如何改变它 package p01; import java.text.DecimalFormat; public class Book { DecimalFormat formatter=new DecimalFormat("##0.00"); private int bookNumber; private String title; privat
package p01;
import java.text.DecimalFormat;
public class Book
{
DecimalFormat formatter=new DecimalFormat("##0.00");
private int bookNumber;
private String title;
private String author;
private int numberOfPages;
private String ISBN;
private double price;
public Book(int bookNumber, String title, String author, int numberOfPages,String ISBN, double price)
{
super();
this.bookNumber = bookNumber;
this.title = title;
this.author = author;
this.numberOfPages = numberOfPages;
this.ISBN = ISBN;
this.price = price;
}
private final int validBookNumber(int bookNumber)
{
if(bookNumber<0||bookNumber>7)
{
throw new IllegalArgumentException("The book number must be from 1 to 7");
}
return bookNumber;
}
private final String validTitle(String title)
{
if (title==null)
{
throw new IllegalArgumentException("The title can't be null");
}
title.trim();
if(title.length()==0)
{
throw new IllegalArgumentException("The title can't be blank");
}
return title;
}
private final String validAuthor(String author)
{
if (author==null)
{
throw new IllegalArgumentException("The name of the author can't be null");
}
author.trim();
if(author.length()==0)
{
throw new IllegalArgumentException("The name of the author can't be blank");
}
return author;
}
private final String validISBN(String ISBN)
{
if (ISBN==null)
{
throw new IllegalArgumentException("The ISBN can't be null");
}
ISBN.trim();
if(ISBN.length()==0)
{
throw new IllegalArgumentException("The ISBN can't be blank");
}
return ISBN;
}
private final int validNumberOfPages(int numberOfPages)
{
if(numberOfPages<=0)
{
throw new IllegalArgumentException("The number of pages can't be less than 0");
}
return numberOfPages;
}
private final double validPrice(double price)
{
formatter.format(price);
if(price<=0)
{
throw new IllegalArgumentException("The price can't be less than 0");
}
return price;
}
public boolean equals(Object obj)
{
if(obj != null && obj instanceof Book)
{
Book tempBook=(Book)obj;
if(ISBN.equalsIgnoreCase(tempBook.ISBN))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public String toString()
{
String str;
str=String.format("%-4s %-11s %-11s %-11s %-11s %-3s",validBookNumber(bookNumber),validTitle(title),validAuthor(author),
validPrice(price), validNumberOfPages(numberOfPages), validISBN(ISBN));//I used the String formatter here
return str;
}
这是我在Simon回答后的输出
Index Book# Title Author Price Pages ISBN
0 1 To kill a Mocking Bird Harper Lee 16.99 336 9780061120084
1 2 Fahrenheit 451 Ray Bradbury 15.99 256 9781451673319
数据中的title/author值似乎比String.format调用中分配的空间长。将长度从11增加到更高的值,例如:
"%-4s %-40s %-25s %-11s %-11s %-3s"
格式字符串中的宽度是最小字符数,因此值将至少填充到该宽度,但如果它们更长,则将输出整个值。也就是说,除非使用精度值指定最大字符数,例如:
String.format("%-4s %-11.11s %-11.11s %-11.11s %-11.11s %-3s");
if (title.length() > 11)
title = title.substring(0, 10) + "\u2026";
这将把第2到第5个值截断为11个字符。但是请注意,它只是这样做的,只需去掉这个值,所以杀死一只知更鸟就变成了杀死一个M,这可能是你想要的,也可能不是你想要的。您可以按照Simon的建议增加宽度和精度,或者自己进行截断并添加省略号或其他内容,例如:
String.format("%-4s %-11.11s %-11.11s %-11.11s %-11.11s %-3s");
if (title.length() > 11)
title = title.substring(0, 10) + "\u2026";
我试过使用它,但使用后仍然没有对齐。我有它打印在上面的问题现在你有一个不同的问题-标题行不匹配。使用相同的字符串格式打印标题行的列。我已经更改为使用相同的格式,但在书号之后,没有一个对齐。因此,我的最终建议是确保列宽至少足够长,以容纳标题名称。e、 g.将书本宽度从%-4s更改为%-6s。在String.format之外写入索引也可能是一个问题。我试图查找有关如何增加宽度的信息,但没有一个信息可以明确表示格式中的宽度?这只是%后面的第一个数字,如果你有标志和参数,那么在你的例子中是11。将11更改为更大的值以增加最小宽度并修复标题,虽然我会修改标题打印以使用相同的格式字符串,但是如果修改另一个,则不必修改其中一个。下面是格式化字符串语法的方法。