简体   繁体   English

从文件读入并添加到2D数组

[英]Reading in from a file and adding to 2D array

I have two different programs, one which contains a method "addGrade" designed to add a new grade to a 2D array (gradeTable). 我有两个不同的程序,其中一个包含“ addGrade”方法,该方法旨在向2D数组(gradeTable)添加新的成绩。 One array of the 2D array is the category each grade should be in, and the second element is the grades for each category. 2D数组的一个数组是每个年级应位于的类别,第二个元素是每个类别的年级。 Here is that program: 这是该程序:

public class GradeBook {


private String name;
private char[] categoryCodes;
private String[] categories;
private double[] categoryWeights;
private double[][] gradeTable;

public GradeBook(String nameIn, char[] categoryCodesIn, 
  String[] categoriesIn, double[] categoryWeightsIn) {

  name = nameIn;
  categoryCodes = categoryCodesIn;
  categories = categoriesIn;
  categoryWeights = categoryWeightsIn;
  gradeTable = new double[5][0];
}

public boolean addGrade(String newGradeIn) {     

  char row = newGradeIn.charAt(0);
  int grade = Integer.parseInt(newGradeIn.substring(1));

  double[] oldArr = gradeTable[row];
  double[] newArr = Arrays.copyOf(oldArr, oldArr.length + 1);
  newArr[newArr.length - 1] = grade;
  gradeTable[row] = newArr;

  return row != 0; 
}

The second program reads in a file as a command argument. 第二个程序读取文件作为命令参数。 The bolded text represents the grades being read in. The letter stands for that category each grade should be in, and the number is the actual grade. 粗体字表示要读的年级。字母代表每个年级应在的类别,数字是实际的年级。 The file is 该文件是

Student1  
5  
a Activities 0.05  
q Quizzes 0.10  
p Projects 0.25  
e Exams 0.30  
f Final 0.30  
**a100 a95 a100 a100 a100  
q90 q80 q100 q80 q80 r90  
p100 p95 p100 p85 p100  
e77.5 e88  
f92**  

In the second program, I'm trying to loop through each grade in the file and call the addGrade method on it so it will be added to the 2D array. 在第二个程序中,我试图遍历文件中的每个成绩并对其调用addGrade方法,以便将其添加到2D数组中。 I'm unsure of how to call the method for each individual grade. 我不确定如何为每个年级调用方法。 Also, I'm pretty sure my addGrade method isn't right. 另外,我很确定我的addGrade方法不正确。 Any help would be appreciated. 任何帮助,将不胜感激。 This is the second program: 这是第二个程序:

public class GradeBookApp {

  String fileName = "";
  String name = "";
  char[] categoryCodes = new char[5];
  String[] categories = new String[5];
  double[] categoryWeights = new double[5];
  double[][] gradeTable;


  if (args.length > 0) {

     for (int i = 0; i < args.length; i++) {
        System.out.println("Reading file \"" + args[i] + "\"." 
           + "\n\tCreating GradeBook object."
           + "\n\tAdding grades to GradeBook object."
           + "\nProcessing of file complete.");


        fileName = args[i];
        Scanner scanFile = new Scanner(new File(fileName));

        name = scanFile.nextLine();
        int catCodes = Integer.parseInt(scanFile.nextLine());

        for (i = 0; i < catCodes; i++) {
           String[] all = scanFile.nextLine().split(" ");
           if(all.length == 3 && all[0].length() == 1 && all[2].matches("(\\d+\\.\\d+)")){
              categoryCodes[i] = all[0].charAt(0);
              categories[i] = all[1];
              categoryWeights[i] = Double.parseDouble(all[2]);
           }
        }

        GradeBook myGB = new GradeBook (name, categoryCodes, 
           categories, categoryWeights);

You'd be better off with having each list of grades as an ArrayList<Double> rather than a double[] . 将每个成绩列表作为ArrayList<Double>而不是double[]会更好。 It's very hard work on the JVM (and on the programmer!) having to copy the whole array each time so that you can increase its length and add a new one. 在JVM(和程序员!)上,这是非常艰巨的工作,每次都必须复制整个数组,以便您可以增加其长度并添加一个新数组。 If you use an ArrayList<Double> gradeList , then you can just 如果使用ArrayList<Double> gradeList ,则可以

gradeList.add(grade);

without needing to do all the copying. 无需进行所有复制。

I would also consider having the larger structure as a Map rather than an array. 我还考虑将较大的结构作为Map而不是数组。 So rather than having a two-dimensional array, you could have a HashMap<Character,List<Double>> that maps the row onto the list of grades for that row. 因此,您可以拥有一个HashMap<Character,List<Double>>而不是一个二维数组,该数组将行映射到该行的成绩列表上。 That avoids having to convert between characters and doubles, which you're currently (implicitly) doing. 这样可以避免在字符和双精度字之间进行转换,而您正在(隐式地)执行此操作。

Finally, the addGrade() method ought to take a char and a double (a row and a new grade), rather than a String : you're making a lot of work for yourself with having to process inappropriate data structures. 最后, addGrade()方法应该采用chardouble (一行和一个新的成绩),而不是String :您不得不处理大量不合适的数据结构,从而为自己做了很多工作。

Once you've done this, calling addGrade for each item should be fairly easy. 完成此操作后,为每个项目调用addGrade应该非常容易。 Once you've extracted a String representing a particular grade (say, String gr = "e77.5" ) then you can add to the list inside your HashMap gradeMap like this: 提取表示特定等级的String (例如, String gr = "e77.5" ),您可以像这样将其添加到HashMap gradeMap的列表中:

char row = gr.charAt(0);
double grade = Double.parseDouble(gr.substring(1));
gradeMap.get(row).add(grade);

I think you'll need to supply more info if you need more help than that. 我认为,如果您需要更多帮助,则需要提供更多信息。

You stated that you need to read from a file, but none of your code is actually reading from a file. 您声明需要从文件读取,但是实际上没有任何代码从文件读取。 This should be your first step. 这应该是您的第一步。 Try looking at BufferedReader documentation as well as numerous posts on this site regarding proper methods to perform file IO operations. 尝试查看BufferedReader文档以及该网站上有关执行文件IO操作的正确方法的大量文章。

I'm assuming your storing your grades in the 2D gradeTable array like: {Category, grade} . 我假设您将成绩存储在2D gradeTable数组中,例如: {Category, grade} You will need to read each row in your file(BufferedReader has methods for this), parse the string (Look at the String documentation, specifically split or substring/indexOf methods) into category and grade, and then populate your array. 您将需要读取文件中的每一行(BufferedReader拥有此方法),将字符串(查看String文档,特别是split或substring / indexOf方法)解析为类别和等级,然后填充数组。

Look into using more dynamic data structures, such as ArrayList . 考虑使用更多动态数据结构,例如ArrayList This will allow you to expand the size as you add more grades, as well as not having to copy your array into a new array every time the size expands. 这样,您可以在添加更多等级时扩展大小,并且不必在每次扩展大小时将阵列复制到新阵列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM