gpt4 book ai didi

java - 如何获得将二维数组打印到java中的两个文件中的函数?

转载 作者:行者123 更新时间:2023-11-30 07:43:51 26 4
gpt4 key购买 nike

我的目标是获取一个包含名称和数字的文件,第一行将命名列,后面的行将是数据。第 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");
}
}

最佳答案

我不明白你的两个输出文件之间的区别,但我认为你的处理方法应该如下所示:

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();
}

关于java - 如何获得将二维数组打印到java中的两个文件中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34221426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com