我试图将单个字符串和二维数组输出到.txt文件中,但遇到了问题,这是我项目的最后一部分。程序应该输出一个逗号分隔的单行字符串,然后在下一行上,打印出2D数组中的双精度数。我应该能够将所有这些打印到.txt文件中,然后在excel文件中打开该.txt文件以绘制信息。这是我到目前为止所知道的,我知道可能会有比我看到的更多的错误:
public void writeFile(double[][] array, String filename)
{
try{
File f = new File("output.txt");
Scanner scan = new Scanner(f);
scan.useDelimiter("\\s*,\\s*");
String label = "Algoritm-1, Algorithm-2, Algorithm-3, "
+ "n, n-squared, n-cubed"; //Only one printing of this line
for (int i = 0; i <= 18; i++)
{
for (int j = 0; j <= 5; j++)
{
array[i][j] = scan.nextDouble(); //Having the current issue
}
scan.nextLine();
}
}
catch (FileNotFoundException fnf) {
System.out.println(fnf.getMessage());
}
}发布于 2012-03-30 23:31:05
看起来您正在对输出文件使用扫描仪。扫描仪通常用于读取输入文件。打开一个新的输出文件,并将数组打印到该文件。
查找OutputStreamWriter的示例。这里有一个:http://www.javapractices.com/topic/TopicAction.do?Id=42
建议:不使用18和5作为循环,可以使用数组维数的长度吗?
发布于 2012-03-30 23:30:41
Scanner用于读取文件/流,但您希望输出到该文件。改为使用一个Writer类(例如,可能与BufferedWriter结合使用的FileWriter )
https://stackoverflow.com/questions/9945960
复制相似问题