-2

The problem is reading back an r,g,b and setting the r,g,b into a setBackground colour when reading from a csv file (comma separated values file) but the csv file also has text in it that needs to be set to the text fields.

the save button saves the file into a csv format that then needs to be read back into the fields which is just a 2d array of text fields.

public void writeDataFile(String fileName)
{
    try
    {

        BufferedWriter outFile = new BufferedWriter(new FileWriter("e:\\EmissionsTracker.csv"));
        for (int x = 0; x < totalX; x++)
        {
            for (int y = 0; y < totalY ; y++)
            {
                outFile.write(fields[0][y].getText() + "," + fields[x][0].getText() + "," + fields[x][y].getText() + ",");
                outFile.write(fields[x][y].getBackground() + ",");



                outFile.newLine();
            }
        }
        outFile.close();

    }
 public void saveEmmisionsTableToFile(String fileName)
{
    try
    {
        BufferedWriter outFile = new BufferedWriter(new FileWriter(fileName));
        for (int y = 0; y < totalY; y++)
        {
            for (int x = 0; x < totalX; x++)
            {
                outFile.write(fields[x][y].getBackground() + "," );               
            }

            outFile.newLine();
        }
        outFile.close();

    }
    catch (Exception e)
    {
        System.err.println("Error: " + e.getMessage());
    }
}
private void readDataFile(String fileName)
{
    try
    {
        BufferedReader br = new BufferedReader(new FileReader(fileName));

        for (int x = 0; x < totalX; x++)
        {
            for (int y = 0; y < totalY; y++)
            {
                String temp[] = br.readLine().split(","); 
                fields[x][y].setText(temp[5]);
            }
        }
        br.close();
    }
    catch (Exception e)
    {
        System.err.println("Error: " + e.getMessage());            
    }
}
wolfsouls
  • 11
  • 7

2 Answers2

-1
for (int x = 0; x < totalX; x++)
        {
            for (int y = 0; y < totalY ; y++)
            {
                outFile.write(fields[0][y].getText() + "," + fields[x][0].getText() + "," + fields[x][y].getText() + ",");
                outFile.write(fields[x][y].getBackground().getRed() + "," + fields[x][y].getBackground().getGreen() + "," + fields[x][y].getBackground().getBlue() + ",");
                outFile.newLine();
            }
        }
for (int x = 0; x < totalX; x++)
        {
            for (int y = 0; y < totalY; y++)
            {

                String temp[] = br.readLine().split(","); 
                fields[x][y].setText(temp[2]);

                fields[x][y].setBackground(new Color(Integer.parseInt(temp[3]),Integer.parseInt(temp[4]),Integer.parseInt(temp[5])));

            }
        }
wolfsouls
  • 11
  • 7
-2

CSV files that contain commas in their values should start and end with double quotes. This statement from RFC 4180 summarises it well:

  1. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:

    "aaa","b CRLF

    bb","ccc" CRLF

    zzz,yyy,xxx

Any half-decent CSV parsing library will take care of this for you automatically.

See also So You Want To Write Your Own CSV code? and these two questions which contain Java CSV library recommendations

Community
  • 1
  • 1
Catchwa
  • 5,845
  • 4
  • 31
  • 57
  • the first line from the csv file when reading reads this javax.swing.plaf.ColorUIResource[r=255 this is the whole line 14,1,14,javax.swing.plaf.ColorUIResource[r=255,g=255,b=255] – wolfsouls Oct 26 '17 at 00:29
  • ...so the row should actually look like `14,1,14,"javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]"` otherwise it's not really a CSV file – Catchwa Oct 26 '17 at 00:32
  • Or instead of writing out a string representation of `ColorUIResource`, why not just write out the red, green and blue values as separate CSV columns? Seems like you're making your life unnecessarily difficult. – Catchwa Oct 26 '17 at 00:35
  • no but i dont want the javax.swing.plaf.ColorUIResource cause then i cant read the data back and set the colour and that line of code is gotten via a method of getBackground() which is from a library – wolfsouls Oct 26 '17 at 00:36
  • Assuming that `getBackground()` returns a `ColorUIResource`, just use `getBackground().getGreen()`, `getBackground().getBlue()`, etc.? https://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/ColorUIResource.html – Catchwa Oct 26 '17 at 00:39
  • ah i see that does fix it didnt know that existed thank you – wolfsouls Oct 26 '17 at 00:45
  • is there a way to return the colour name? or would i just have to check what the numbers are and set something to the be the colour name if the number match that format – wolfsouls Oct 26 '17 at 00:46
  • I would assume that whatever library you're using would also have a `setBackground()` method also? Use the API link I posted to construct a new ColorUIResource based on the separate RGB values you've read in. – Catchwa Oct 26 '17 at 01:12