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)
How to solve this issue? Thanks.
Change your calls to
append(Matrix[i][j])toappend(String.valueOf(Matrix[i][j])orappend("" + Matrix[i][j]). The problem (as the error message points out) is that you are attempting to append an integer, but the append method only take a CharSequence (i.e. a String). Both of the solutions I present coerce the integer/numeric type to a String.