【发布时间】:2015-11-16 02:24:42
【问题描述】:
我试图让我的程序以简洁的格式打印出数据,数据与列对齐。我尝试在我的类的 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
这是我在西蒙回答后的输出
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
【问题讨论】: