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.

Recommended Answers

All 6 Replies

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

Member Avatar for hfx642

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.)

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(); 
}
Member Avatar for hfx642

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.)

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

Member Avatar for hfx642

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.