java - 如何获得将二维数组打印到java中的两个文件的功能?
Posted
技术标签:
【中文标题】java - 如何获得将二维数组打印到java中的两个文件的功能?【英文标题】:How do I get the function to print out a 2D array into two files in java? 【发布时间】:2016-03-17 05:48:00 【问题描述】:我的目标是获取一个包含名称和数字的文件,第一行将命名列,后面的行将是数据。列 0 将具有名字,而 1 将具有将组合的姓氏。第 2、3 和 4 列将包含 3 个将一起平均的数字,第 5 到 11 列也将包含将一起平均的数字。这将是测试数据:
first, last, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10
Dan, Smith, 56, 58, 55, 66, 44, 78, 60.33, 52.33, 70.66, 44
Sam, Will, 77, 88, 55, 99, 77, 10, 62, 65, 59, 62
从这里开始,两个输出文件将包含相同的信息,第二个输出文件将以更整洁的方式分隔信息。我想要的输出是:
name, exam score,hw score,min score
Dan Smith, 56.33, 62.66, 56.33
Sam Will, 73.33, 62, 62
这一行将只用于第二个输出文件:
out.printf("%20s: %10.2f, %8.2f, %9.2f\r\n",name, average1, average2, smallestaverage);
我的代码如下,我尝试测试看看是否可以使用 for 循环打印出部分数据,但只能接收为两个变量设置的原始值:
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.PrintWriter;
public class compute_grades
public static void process_grades(String input_name, String csv_name, String pretty_name)
String[][] data = read_spreadsheet(input_name);
String csvname = "csv_name.txt";
String pretty = "pretty_name.txt";
PrintWriter out = null;
try
out = new PrintWriter(input_name);
catch (Exception e)
System.out.printf("Error: failed to open file %s.\n", input_name);
System.exit(0);
String first = "dog";
String last = "dat";
for (int i = 0; i < data.length; i++)
first = data[i][0];
last = data[i][1];
System.out.println(first + last);
public static String[][] read_spreadsheet(String filename)
ArrayList < String > lines = read_file(filename);
if (lines == null)
return null;
int rows = lines.size();
String[][] result = new String[rows][];
for (int i = 0; i < rows; i++)
String line = lines.get(i);
String[] values = line.split(",");
result[i] = values;
return result;
public static ArrayList < String > read_file(String filename)
File temp = new File(filename);
Scanner input_file;
ArrayList < String > result = new ArrayList < String > ();
try
input_file = new Scanner(temp);
catch (Exception e)
System.out.printf("Failed to open file %s\n", filename);
return result;
while (input_file.hasNextLine())
String line = input_file.nextLine();
result.add(line);
input_file.close();
return result;
public static void main(String[] args)
Scanner in = new Scanner(System. in );
System.out.printf("Please enter the name of the input file: ");
String input_name = in .next();
System.out.printf("Please enter the name of the output CSV file: ");
String csv_name = in .next();
System.out.printf("Please enter the name of the output pretty-print file: ");
String pretty_name = in .next();
process_grades(input_name, csv_name, pretty_name);
System.out.printf("\n\nExiting...\n");
【问题讨论】:
【参考方案1】:我不明白你的两个输出文件之间的区别,但我认为你的过程方法应该是这样的:
public static void process_grades(String input_name, String csv_name, String pretty_name)
String[][] data = read_spreadsheet(input_name);
PrintWriter out = null;
try
out = new PrintWriter(csv_name);
catch (Exception e)
System.out.printf("Error: failed to open file %s.\n", input_name);
System.exit(0);
// print header
out.printf("name, exam score,hw score,min score\r\n");
// go throu all the other rows
for (int row = 1; row < data.length; row++)
// get Name
String name = data[row][0] + " "+ data[row][1];
// avg the next three cols
double average1 = 0;
for(int col = 2; col < 5; col++)
average1 += Double.parseDouble(data[row][col].trim());
average1 /= 3;
// avg the next seven cols
double average2 = 0;
for(int col = 5; col < 12; col++)
average2 += Double.parseDouble(data[row][col].trim());
average2 /= 7;
//get min score
double smallestaverage = Math.min(average1,average2);
// print the content
out.printf("%20s: %10.2f, %8.2f, %9.2f\r\n",name, average1, average2, smallestaverage);
// flush the output and close the writer
out.flush();
out.close();
【讨论】:
我稍微修改了一下,以便创建两个输出文件。PrintWriter out1 = null; PrintWriter out2 = null; try out1 = new PrintWriter(csv_name); out2 = new PrintWriter(pretty_name);
etc 等只是添加了第二个输出并更改了标签的初始 out.printf 语句和最后的 out.printf 以便它在两个文件上打印相同的信息以上是关于java - 如何获得将二维数组打印到java中的两个文件的功能?的主要内容,如果未能解决你的问题,请参考以下文章