2014-09-25 93 views
1

我想知道如何格式化toString: 我尝试使用printf和S tring.format,但要我改变StringintObject[]格式的toString的Java

我希望它看起来是这样的:(空格)

Book1______Author1________850

Book23_____Author2424_____250

class Book 
{ 
    private String title,author; 
    private int numberOfPages; 

    Book() 
    { 

    } 

    public Book(String title, String author, int pages) { 
     this.title = title; 
     this.author = author; 
     this.numberOfPages = pages; 
    } 

    public String toString() 
    { 
     return(title + "%63s") + "\t" + (author + "%63s") + "\t" + numberOfPages; 
     // It actually works but makes the "%63s" appear :o 
    } 
} 
+2

'%63s'只是您连接的字符串文字。这些标签是由'“\ t”'添加的,因此您可以简单地删除'%63s',但如果您的书名和作者长度不同,这很可能不会对齐。编辑:你想让书籍集合一致吗?如果是这样,你不能为个别的toString方法做到这一点。 – 2014-09-25 22:18:14

+0

你应该使用'String.format'。有什么问题? – 2014-09-25 22:22:59

+0

“String类型的方法格式(Locale,String,Object [])不适用于参数(String,String,String,int)” :( – user3764862 2014-09-25 22:27:29

回答

5
return String.format("%-30s%-30s%10d", title, author, numberOfPages); 
+0

我试过,它说: “The方法格式(区域设置,字符串,对象[])在字符串类型不适用于参数(字符串,字符串,字符串,整数)“:/ – user3764862 2014-09-25 22:26:28

+0

@ user3764862您使用什么版本的Java? – Brian 2014-09-25 22:28:55

+2

http:// ideone的.com /叉/ khe1Wc – maszter 2014-09-25 22:35:40

0

的String.format是正确的。你只需要正确的格式化字符串。

更新您的toString()方法如下:

public String toString() 
{ 
    return String.format("%63s\t%63s\t%10d", title, author, numberOfPages); 
} 

第一%63stitle被替换。

%63s将被替换为author并添加空格使其超过标题末尾的63个字符。

%10d替换为numberOfPages,并将数字填充到10位宽。

0

我不太确定您是否希望将标题,作者和页数以三个单独的列对齐。但是,如果你这样做,你不能通过重写Book的toString方法来实现这一点,因为它不知道集合中其他书籍的标题和作者名称有多长。

但是,您可以为书籍声明自定义集合并覆盖其toString方法。这可能如下所示:

class Main { 

static class Book 
{ 
    private String title,author; 
    private int numberOfPages; 

    public Book(String title,String author,int pages) { 
     this.title = title; 
     this.author = author; 
     this.numberOfPages = pages; 
    } 

    public String toString() 
    { 
     return String.format("%-30s%-30s%10d", title, author, numberOfPages); 
    } 

} 

static class Library { 

    public static final int Margin = 2; 

    private List<Book> books = new ArrayList<Book>(); 
    public void addBook(Book book){ 
     books.add(book); 
    } 
    private int longestTitle() { 
     int result = 0; 
     for (Book book : books) { 
      result = book.title.length() > result ? book.title.length() : result; 
     } 
     return result; 
    } 
    private int longestAuthor() { 
     int result = 0; 
     for (Book book : books) { 
      result = book.author.length() > result ? book.author.length() : result; 
     } 
     return result; 
    } 

    public String toString() { 
     String result = ""; 
     for (Book book : books) { 
      int titleLength = book.title.length(); 
      result += book.title; 
      for (int i = longestTitle(); i > titleLength - Margin; i--){ 
       result += " "; 
      } 
      result += book.author; 
      int authorLength = book.author.length(); 
      for (int i = longestAuthor(); i > authorLength - Margin; i--){ 
       result += " "; 
      } 
      result += book.numberOfPages + "\n"; 
     } 
     return result; 
    } 
} 

public static void main(String[] args) { 
    Library lib = new Library(); 
    Book b1 = new Book("Knights of the round table", "King Arthur", 123); 

    lib.addBook(b1); 
    Book b2 = new Book("It", "Stephen King", 1345); 
    lib.addBook(b2); 
    Book b3 = new Book("A very, very, very, very long title that seems pointless", "Me", 112); 
    lib.addBook(b3); 
// System.out.println(b1); 
// System.out.println(b2); 
// System.out.println(b3); 
// They do not align separately 
    System.out.println(lib.toString()); 
} 

} 

的想法是看其标题是最长的,并添加你想要打印至少达到最长的标题填充每一本书。作者也一样。

请注意,这段代码写得不是很好,但它应该足以表达这个想法。