【问题标题】:What is wrong with this String formatter line?这个字符串格式化程序行有什么问题?
【发布时间】:2012-10-10 02:10:18
【问题描述】:
String result = String.format( "% % % | %-20s| %25s | $%,8.2f | %5d | %-s", thisYear, thisMonth, thisDay, 
                    eventName, eventVenue, ticketPrice, ticketsRemaining, eventType);

ints:thisYear、thisMonth、thisDay、ticketRemaining。

字符串:eventName、eventVenue、eventType。

double:ticketPrice。

我已验证所有字段均已正确初始化。此行应输出为以下格式:

2012-01-20 | Penguins-Capitals    |      CONSOL Energy Center | $   92.35 |    15 | NHL
2012-08-05 | Chickenfoot          |    Riverplex Amphitheatre | $   62.50 | 1,273 | Concert

格式化程序不完整,但是当我尝试运行它时出现故障,因此其中存在一些语法错误。我不太擅长格式化程序。

【问题讨论】:

    标签: java string.format


    【解决方案1】:

    您应该确保始终在% 之后添加类型,在本例中为%d

    String result = String.format( "%4d-%02d-%02d | %-20s| %25s | $%.3f | %5d | %s", thisYear, thisMonth, thisDay, 
                    eventName, eventVenue, ticketPrice, ticketsRemaining, eventType);
    

    要制作, 而不是.,请观看此thread

    【讨论】:

    • 谢谢,但是当我尝试调试您修改后的格式化程序时,我仍然遇到一个致命错误。 '$%,8.2f' 字段的格式是否正确?
    • 不,应该是 $%4.3f 或 $%.3f 取决于 ',' 之前的字符
    • 还是不行。这是整个班级(自包含)pastebin.com/gJEM76Dh
    猜你喜欢
    • 2013-08-14
    • 2019-11-09
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    相关资源
    最近更新 更多