我的目标是获取一个带有名称和数字的文件,第一行将命名列,后面的行将是数据。第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");
}
}
答案 0 :(得分: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();
}