GWT Java - 服务器端 "for loop" 不工作

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==ji<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