簡體   English   中英

如何使用嵌套循環與打印編寫器一起打印出特定方式

[英]How to Print out a particular way using nested loops with print writer

我正在嘗試讀取文件並以某種方式打印出信息

這是我目前的方法

public class PrintImpl {

public void createReport(PropertyLogImpl propertyLogImpl, RealtorLogImpl realtorLogImpl, String fileName) {
    System.out.println("Creating clean report..");
    System.out.println("Report is complete -- located in file: " + fileName);

    try {

        PrintWriter writer = new PrintWriter(fileName);
        LinkedList<Property> propertyList = propertyLogImpl.getPropertyList();
        System.out.println("Realtor Log:");
        RealtorNode listTop = realtorLogImpl.getListTop();
        RealtorNode temp = listTop;
        Iterator<Property> iter = propertyList.iterator();
        propertyLogImpl.setPropertyList(propertyList);
        Property property = iter.next();

        while (temp != null) {
            writer.println(temp.getRealtor().getLicenseNumber() + " " + temp.getRealtor().getLastName() + ", " + temp.getRealtor().getFirstName());

            if (temp.getRealtor().getLicenseNumber().compareTo(property.getLicenseNumber()) == 0) {
                writer.println("    " + property.getMlsNumber() + " "
                        + property.getStreetAdress() + " "
                        + property.getBedrooms()
                        + "/" + property.getBathrooms() + "$ "
                        + property.getAskingPrice() + " "
                        + property.isSold());

                writer.println("        " + property.getCity() + ", "
                        + property.getState() + ", " + property.getZipCode());
                if (iter.hasNext()) {
                    property = iter.next();
                }
            }

        }
        temp = temp.getNextNode();

        writer.close();

    } catch (FileNotFoundException ex) {

        System.out.println("File was not found");
        System.out.println();
    }
}

}

但問題是,它陷入了無限循環。 因此,它要做的是將房地產經紀人許可證號與財產許可證號相匹配。 該屬性是一個鏈表,而房地產經紀人是一個類似於鏈表的節點。 基本上,我想做的就是得到一個節點,遍歷屬性並在房地產經紀人上打印出該屬性下的所有屬性。 然后切換到下一個節點並重復。 但是我嘗試過的所有循環都沒有起作用。 請幫忙!

這是該文件的外觀

CD4524521 snart, paul
         1254294         246 wart street       2/3.0   $   456321.00    SOLD
                     somewhere, CO 54236

          1683946       879  Main St            2/2.0   $   646589.00   
               Somehwere, CO 45726

看起來就像只是房地產經紀人的名字,然后繼續為房地產經紀人添加屬性,在沒有更多房地產經紀人屬性之后,切換到新的房地產經紀人並繼續為該房地產經紀人添加屬性。 謝謝

temp = temp.getNextNode(); 在while循環之外。 因此, temp的值永遠不會改變。 這就是為什么出現無限循環的原因,因為while條件是temp != null

另外,我建議您考慮重寫該Property類的toString()方法,或添加另一個方法以獲取格式正確的屬性字符串表示形式。 如果您開始將這些字符串混淆在一起作為while循環中的輸出,您的代碼看起來會很混亂。

@Override
public String toString(){
    return prop1 + "\t" + prop2
        + "whatever" + somefield;
}

除了循環外的temp變量蜂鳴(如@mumpitz所指出的)外,您還可以使用

writer.printf("%s %d    ...","string",1234)

這樣您就可以將格式與字段分開。 StringBuilder也使此操作更加舒適。

如果您有更多復雜的東西要在不久的將來格式化,請查看MessageFormat: https : //docs.oracle.com/javase/tutorial/i18n/format/messageFormat.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM