首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用printf会得到未对齐的列

问使用printf会得到未对齐的列
EN

Stack Overflow用户
提问于 2015-11-16 02:04:12
回答 2查看 117关注 0票数 0

我正在尝试让我的程序以一种整洁的格式打印出数据,并使数据与列对齐。我尝试在我的类的toString方法中使用String.format,但它根本不对齐。我该怎么改呢?

代码语言:javascript
复制
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;
}

}

代码语言:javascript
复制
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));
    }
}

}

这是我得到的输出

代码语言:javascript
复制
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回答后的输出

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2015-11-16 02:09:22

数据中的标题/作者值看起来比在String.format()调用中分配的空间长。将长度从11增加到更高的值,例如:

代码语言:javascript
复制
"%-4s %-40s %-25s %-11s %-11s %-3s"
票数 0
EN

Stack Overflow用户

发布于 2015-11-16 02:21:59

格式字符串中的宽度是最小字符数,因此将填充至少该宽度的值,但如果它们更长,则将输出整个值。也就是说,除非您使用精度值指定最大字符数,例如:

代码语言:javascript
复制
String.format("%-4s %-11.11s %-11.11s %-11.11s %-11.11s %-3s");

这会将第2到第5个值截断为11个字符。但是请注意,它只是去掉了值,所以"To Kill a Mockingbird"变成了"To Kill a M",这可能是您想要的,也可能不是您想要的。您可以增加宽度(如Simon建议的那样)和精度,也可以自己进行截断并添加省略号或其他东西,例如:

代码语言:javascript
复制
if (title.length() > 11)
    title = title.substring(0, 10) + "\u2026";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33723065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档