格式化 toString Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26048995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting toString Java
提问by user3764862
I'd like to know how to format toString
:
I tried using printf
and string.format
but it wants me to change String
and int
to Object[]
我想知道如何格式toString
:我尝试使用printf
和S tring.format
,但要我改变String
,并int
以Object[]
I want it to look like this: (Spaces)
我希望它看起来像这样:(空格)
Book1______Author1________850
Book23_____Author2424_____250
书1______作者1________850
Book23_____作者2424_____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
}
}
回答by maszter
return String.format("%-30s%-30s%10d", title, author, numberOfPages);
回答by Brian
String.format is correct. You just need to get the formatting string correct.
String.format 是正确的。您只需要正确设置格式字符串。
Update your toString()
method as follows:
更新您的toString()
方法如下:
public String toString()
{
return String.format("%63s\t%63s\t%10d", title, author, numberOfPages);
}
The first %63s
will be replaced with title
.
第一个%63s
将替换为title
.
The %63s
will be replaced with author
and add spaces to make it 63 characters past the end of the title.
在%63s
将被替换author
,并添加空格,使其63个字符过去的标题末尾。
The %10d
will be replaced with numberOfPages
and pad the number out to 10 digits wide.
的%10d
将被替换numberOfPages
和垫数出10位宽。
回答by Kulu Limpa
I am not quite sure if you want to align the titles, the authors and the number of pages in three separate columns. But if you do with to do this, you cannot achieve this by overriding the toString
method of Book, because it will not know how long the title and author names of the other books in the collection are.
我不太确定您是否要在三个单独的列中对齐标题、作者和页数。但是如果你这样做,你不能通过覆盖toString
Book的方法来实现这一点,因为它不会知道集合中其他书籍的标题和作者姓名有多长。
You can, however, declare a custom collection for your books and override its toString
method. This could look as follows:
但是,您可以为您的书籍声明一个自定义集合并覆盖其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());
}
}
The idea is to look which title is the longest and add for each book you want to print a padding that reaches at least the longest title. The same for the author.
这个想法是查看哪个标题是最长的,并为每本书添加一个至少达到最长标题的填充。对作者来说也是一样。
Note that this code isn't written really well, but it should suffice to convey the idea.
请注意,这段代码写得不是很好,但应该足以传达这个想法。