我试图让我的程序以整齐的格式打印数据,数据与列对齐。我尝试在我的toString方法中为我的类使用String.format,但它根本没有对齐。我该怎么改变它?
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;
}
}
package p01;
public class MJE_AB_List_Driver01
{
public static void main(String[] args)
{
MJE_AB_List<Book>myBookList=new MJE_AB_List<Book>();
Book book1=new Book(1,"To kill a Mocking Bird", "Harper Lee",336,"9780061120084", 16.99);
Book book2=new Book(2,"Fahrenheit 451", "Ray Bradbury",256," 9781451673319", 15.99);
Book book3=new Book(3,"1984", "George Orwell",304,"9780452262935", 16.00);
Book book4=new Book(4,"Fight Club", "Chuck Palahniuk",224,"0-393-03976-5", 14.95);
Book book5=new Book(5,"Invisible Monsters", "Chuck Palahniuk",304,"9780061120084", 16.99);
Book book6=new Book(6,"Frankenstein", "Mary Shelly",256,"9781593081157", 6.95);
Book book7=new Book(7,"The Grapes of Wrath", "John Steinbeck",528,"9780143039433", 17.00);
myBookList.add(book1);
myBookList.add(book2);
displayList(myBookList);
}
public static void displayList(ParticularListInterface list)//This displays the list
{
String headerString="Index Book# Title Author Price Pages ISBN";
System.out.println(headerString +"\n");
for(int index=0;index<list.size(); index++)
{
System.out.println(index+ " "+list.get(index));
}
}
}
这就是我的输出
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
这是我在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
答案 0 :(得分:0)
您的数据中的标题/作者值似乎比您在String.format()
调用中分配的空间长。将长度从11增加到更高的值,例如:
"%-4s %-40s %-25s %-11s %-11s %-3s"
答案 1 :(得分:0)
格式字符串中的宽度是 minumum 字符数,因此值将被填充为至少那么宽,但如果它们更长,那么整个值将是输出。也就是说,除非您使用精度值指定最大个字符数,例如:
String.format("%-4s %-11.11s %-11.11s %-11.11s %-11.11s %-3s");
这会将第2到第5个值截断为11个字符。但是请注意,它就是这样,只需删除该值,因此"To Kill a Mockingbird"
变为"To Kill a M"
,这可能是您想要的,也可能不是。您可以增加宽度(如Simon建议的那样)和精度,或者自己进行截断并添加省略号或其他内容,例如:
if (title.length() > 11)
title = title.substring(0, 10) + "\u2026";