GWT Java - 服务器端“for loop”无法正常工作

时间:2016-12-11 00:44:28

标签: java for-loop server-side

我正在读取一个文件,其中每行都有一个列号,行号,详细信息。该文件按列然后排序。我想将详细信息放在正确的行和列中的csv文件中。所以我正在测试行号的变化,然后添加换行符(“\ n”)。

问题是for循环每一侧的System.out.println都显示在日志中;但是,它的self不会被触发循环(即,没有添加换行符,并且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”来尝试强制结果。

2 个答案:

答案 0 :(得分:1)

在用于迭代行的语句中,您有

for(int i = currentRow; i > j; i++)

如果j是当前行的数量,那么您需要将条件更改为i < j以完成所有行。

答案 1 :(得分:0)

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