使用BufferedWriter&Loop从2D JTextField数组保存文本

时间:2021-09-18 13:27:03

I have a 2D array called fields and I need to be able to save its contents (colour initials).

我有一个名为fields的2D数组,我需要能够保存其内容(颜色首字母)。

try {
    BufferedWriter outFile = new BufferedWriter(new FileWriter("Drawing_NEW.csv"));
    for (int y = 0; y < totalY; y++) {
        for (int x = 0; x < totalX - 1; x++) {
            outFile.write(fields[x][y].getText() + ",");
        }
        outFile.write(fields[totalX - 1][y].getText());
        outFile.newLine();
    }
    outFile.close();
} catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
}

The code above saves everything in the array like this. Note the array is 20 by 20 (The output below is just a snippet of the whole thing).

上面的代码像这样保存了数组中的所有内容。请注意,数组是20乘20(下面的输出只是整个事情的片段)。

W,W,W,G,G,G,G,G,G,G,G,G,G,G,G,G,G,G,W,W
W,W,W,G,G,G,G,G,G,G,G,G,G,G,G,G,G,G,W,W

But I have to now make a loop where if the colour is the same as the next add one to the counter and if it does not then write the new colour and set the counter back to 1 then check again for the next and so on. below are a sample template and output of what it's supposed to look like.

但我现在必须制作一个循环,如果颜色与下一个添加到计数器的相同,如果没有,则写入新颜色并将计数器设置回1然后再次检查下一个,依此类推。下面是一个示例模板和它应该是什么样的输出。

(colour1,count1, colour2,count2, colour3,count3,)

W,3,G,15,W,2
W,3,G,3,Y,5,G,7,W,2

Feel free to ask questions. Thank you.

随意问的问题。谢谢。

1 个解决方案

#1


2  

It means that you need to add some state to your loops to track previous values. AFAIU from your example, you want to write a number only for the sequence of the same string in the same "line" of the array. If this is so, try following code

这意味着您需要在循环中添加一些状态以跟踪以前的值。从您的示例中的AFAIU,您只想为数组的同一“行”中的相同字符串的序列编写一个数字。如果是这样,请尝试以下代码

for (int y = 0; y < totalY; y++) {
    string prev = ""; // this value doesn't equal anything you can see in the UI, so the first iteration of the loop works as expected 
    int cnt = 0;
    for (int x = 0; x < totalX - 1; x++) {
        string cur = fields[x][y].getText();
        if(cur.equals(prev)) {
            cnt ++;
        }
        else {
            if(cnt > 0) // skip the first empty line
              outFile.write(prev + "," + cnt + ",");
            prev = cur;
            cnt = 1;
        }
    }
    // write the last sequence
    outFile.write(prev + "," + cnt);
    outFile.newLine();
}

#1


2  

It means that you need to add some state to your loops to track previous values. AFAIU from your example, you want to write a number only for the sequence of the same string in the same "line" of the array. If this is so, try following code

这意味着您需要在循环中添加一些状态以跟踪以前的值。从您的示例中的AFAIU,您只想为数组的同一“行”中的相同字符串的序列编写一个数字。如果是这样,请尝试以下代码

for (int y = 0; y < totalY; y++) {
    string prev = ""; // this value doesn't equal anything you can see in the UI, so the first iteration of the loop works as expected 
    int cnt = 0;
    for (int x = 0; x < totalX - 1; x++) {
        string cur = fields[x][y].getText();
        if(cur.equals(prev)) {
            cnt ++;
        }
        else {
            if(cnt > 0) // skip the first empty line
              outFile.write(prev + "," + cnt + ",");
            prev = cur;
            cnt = 1;
        }
    }
    // write the last sequence
    outFile.write(prev + "," + cnt);
    outFile.newLine();
}