在try里面循环打印

时间:2014-02-08 07:25:05

标签: java loops for-loop try-catch

我正在尝试在try和catch块中打印一个数组,最后将其导出到.csv文件,但for循环会出错:

for(y=0; y<rows; y++){ //Error here
            writer.append("" + sum[y]); // and here: that "y is not public..."
            writer.append(',');
}

整个尝试和捕获块:

try
    {
        FileWriter writer = new FileWriter(sFileName); 
        writer.append("DisplayName");
        writer.append(',');
        writer.append("Age");
        writer.append('\n');

        writer.append("MKYONG");
        writer.append(',');
        writer.append("26");
            writer.append('\n');

            for(y=0; y<rows; y++){
                writer.append("" + sum[y]);
            writer.append(',');
            }
            writer.append('\n');

        writer.append("YOUR NAME");
        writer.append(',');
        writer.append("29");
        writer.append('\n');

            writer.append("");

        //generate whatever data you want

        writer.flush();
        writer.close();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:1)

您错过了提供变量数据类型y

    for(int y=0; y<rows; y++){ //Error here
 --------^
                writer.append("" + sum[y]); // and here: that "y is not public....."
                writer.append(',');
    }