[英]GWT Java - Server Side “for loop” not working
我正在閱讀一個文件,其中每一行都有一個列號,行號,詳細信息。 該文件按列然后按行排序。 我想將詳細信息放置在csv文件中正確的行和列中。 因此,我正在測試行號的更改,然后添加換行符(“ \\ n”)。
問題是for循環兩邊的System.out.println都顯示在日志中。 但是,循環本身不會被觸發(即,未添加換行符,並且System.out.println不會出現在日志中。
代碼是:
System.out.println("New row - " + Integer.parseInt(report.getReportDetailRow())+ " greater than current row - " + currentRow);
currentCol = 0;
//Add line breaks
int j = Integer.parseInt(report.getReportDetailRow());
for(int i = currentRow; i > j; i++){
System.out.println("Append line break");
fileContent.append("\n");
}
System.out.println("After append");
currentRow = Integer.parseInt(report.getReportDetailRow());
if (currentCol == Integer.parseInt(report.getReportDetailColumn())){
fileContent.append(report.getReportDetailDetails() + ",");
currentCol++;
}else{
//Add columns
for(int i = currentCol; i == Integer.parseInt(report.getReportDetailColumn()); i++){
fileContent.append(",");
}
fileContent.append(report.getReportDetailDetails() + ",");
currentCol = Integer.parseInt(report.getReportDetailColumn());
}
請注意,我已經使用“ i> j”而不是“ i == j”來強制執行結果。
在遍歷行的語句中,您有
for(int i = currentRow; i > j; i++)
如果j
是當前行的數量,則需要將條件更改為i < j
來遍歷所有行。
for(int i = currentRow; i > j; i++) {
System.out.println("Append line break");
fileContent.append("\n");
}
上面的循環將導致無限循環或永遠不會被觸發(您的情況)
i
已經大於j
則為無限。 每次迭代它都不會以i++
終止 i
小於j
,則永遠不要執行,因為條件狀態i>j
。 您可能想要在循環內更改條件語句,以將其更正為i==j
或i<j
for(int i = currentRow; i == j; i++) // in which case replacing this with an `if(i==j)` would do the needful
要么
for(int i = currentRow; i < j; i++) // to iterare from initial i upto j
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.