0

I am trying to save an integer matrix to the csv file. My code is listed as follows.

try
        {
          FileWriter writer = new FileWriter("test.csv");
          
             for(int i = 0; i < row; i++)
             {
               
                 for (int j=0; j<(column-1); j++)
                 {
                	 writer.append(Matrix[i][j]);
                	 writer.append(',');
                 }
            	
                   writer.append(Matrix[i][j]);
                   writer.append('\n');
                   writer.flush();
             }
             writer.close();
          }
        
        catch(Exception e)
        {
          e.printStackTrace();
        }

However, the Eclipse gives the following error message:

method append(CharSequence) in the type Writer is not
applicable for the arguments (int)

Please let me know how to fix this issue. Thanks.

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by hfx642
0

Use String.valueOf(int) to convert your int to a string for the writer.

Edited by Ezzaral: n/a

0

Start with this...

BufferedWriter Text_Output;
   String File_Out = "Output.csv";

      try
      { Text_Output = new BufferedWriter (new FileWriter (File_Out, Append)); }
      catch (IOException IOE)
      {  }

      try
      {
         Text_Output.write ("Data, Data, Data");
         Text_Output.newLine ();
      }
      catch (IOException IOE)
      {  }

      try
      { Text_Output.close (); }
      catch (IOException IOE)
      {  }

and modify it to suit your needs (looping, etc.)

0

Absolutely do NOT start with any code that has

catch (IOException IOE)
{ }

If it doesn't work because of an Exception you'll never be able to debug it.
At the very least replace all those with

catch (IOException e) { 
  e.printStackTrace(); 
}

Edited by JamesCherrill: n/a

0

Whoa... Easy James!
I was expecting them to fill in their own code in the exception catches.
It was just a framework to get them started.
(I'm not going to give them MY code to handle the exceptions.)

Edited by hfx642: n/a

0

Hi hhfx642, We have an awful lot of absolute beginners here, and leaving catch clauses blank is one of the mistakes they often make (with obvious consequences). I wasn't having a go at you, I was just worried about newbies taking that template as a starting point without realising the importance of diagnosing exceptions. :-)
J

0

Understood! :)
Next time, I'll make it more obvious for them to put their own code in where appropriate.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.