【问题标题】:String.format() doesnt format the output correctString.format() 没有正确格式化输出
【发布时间】:2017-06-11 05:16:41
【问题描述】:

我有一个问题,我的输出不正确,我不知道如何解决。

在这张图片中,您可以看到标题 Januar 2017 。 . . .稍后推迟。

Januar 2017            Februar 2017            Maerz 2017            
So|01|            |1    Mi|01|            |32    Mi|01|            |60    
Mo|02|            |2    Do|02|            |33    Do|02|            |61    
Di|03|            |3    Fr|03|            |34    Fr|03|            |62    
Mi|04|            |4    Sa|04|            |35    Sa|04|            |63    
Do|05|            |5    So|05|            |36    So|05|            |64    
Fr|06|            |6    Mo|06|            |37    Mo|06|            |65    

我正在尝试这样做。月份名称应从 Weekdayname(short) So Mo Di Mi Do Fr Su 的点开始

Januar 2017             Februar 2017             Maerz 2017            
So|01|            |1    Mi|01|            |32    Mi|01|            |60    
Mo|02|            |2    Do|02|            |33    Do|02|            |61    
Di|03|            |3    Fr|03|            |34    Fr|03|            |62    
Mi|04|            |4    Sa|04|            |35    Sa|04|            |63    
Do|05|            |5    So|05|            |36    So|05|            |64    
Fr|06|            |6    Mo|06|            |37    Mo|06|            |65    

这是我如何构建monthPaper的代码

public LinkedList<String> buildMonth(int month) {
    int lengthOfMonth = calender.getLengthOfMonth(this.year, month);
    LinkedList<String> monthList = new LinkedList<>();
    monthList.add(String.format("%-10s",monthname[month]) + String.format("%-17s", this.year));

    for (int day = 1; day <= lengthOfMonth; day++) {
        int daynumber = kf.daynumber(day, month, this.year);
        int weekday = kf.wochentag_im_jahr(this.year, daynumber);


            String monthPlan = weekdayShort[weekday] + "|" + String.format("%02d",day) +"|\t\t\t|" +String.format("%-10s",calender.getTwoDigit(daynumber));
            monthList.add(monthPlan);


    }
    return monthList;
}

【问题讨论】:

  • #1:我不知道您所说的“标题 Januar ... 推迟了一段时间”是什么意思。也许“推迟”是错误的词?我需要更清楚的细节来说明问题所在。如果您发布您希望输出的样子,作为示例(并且 not 作为图像)以及实际输出(not 作为图像),这可能会有所帮助),所以我们可以看到问题所在。 #2:我们希望尽可能将示例输出作为文本而不是图像包含在问题中。在这种情况下,我不得不将图像放大到 250%,但仍然无法阅读。
  • 阅读minimal reproducible example。是的,不鼓励使用屏幕截图。
  • 对不起,我不知道它缩放了这么多图片
  • @ManuellsenDeve,看起来这个问题只有你问过。在stackoverflow.com/questions/44476433/…
  • 我被封号了。我试图弄清楚如何解决这个问题 vishal

标签: java linked-list formatting string.format


【解决方案1】:

我认为问题在于/t 的长度在不同的系统中可能会有所不同,您应该更改天字符串的格式。试着用这种方式来做

System.out.println(String.format("%s|%02d|%20s%-6s", weekdayShort[weekday], day, "|", calender.getTwoDigit(daynumber)));

【讨论】:

  • 它可以工作并且具有相同的宽度。但是标题不在正确的行中。我试试
  • 好的,谢谢尤里。问题出在标签上。
猜你喜欢
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
相关资源
最近更新 更多