0

目前,我正在尝试读取 .dat 文件并将各种行分配到数组中。该文件将提供诸如“a100”和“q80”之类的项目,我必须按字母将它们分成类别,然后将不同的等级作为每个类别的数组。现在,这就是我所拥有的,但是当我尝试各种事情时,我遇到了很多运行时错误。我在这里缺少什么吗?

我遇到的一些错误:

  1. 当我执行案例'P'时,它会打印出来:WeightedGrades@13105f32
  2. 当我尝试执行案例 C、A 或 D 时,会发生这种情况:线程“主”中的异常java.lang.NoSuchMethodError: WeightedGrades.deleteGrade(Ljava/lang/String;)Z

加权等级:

public class WeightedGrades {
   private String name;
   private int numGrades;
   private String[] grades;
   public static final double ACTV_WT = 0.05, QUIZ_WT = 0.10, PROJ_WT = 0.25, EXAM_WT = 0.30, FINAL_EXAM_WT = 0.30;

   public WeightedGrades(String nameIn, int numGradesIn, String[] gradesIn) {
      name = nameIn;
      numGrades = numGradesIn;
      grades = gradesIn;
   }

   public String getName() {
      return name;
   }
   public int getNumGrades() {
      return numGrades;
   }
   public String[] getGrades() {
      return grades;
   }
   public double[] gradesByCategory(char categoryChar) {
      int count = 0;
      for (int i = 0; i < grades.length; i++) {
         if (categoryChar == grades[i].charAt(0)) {
            count++;
         }
      }
      double[] gradesNew = new double[count];
      count = 0;
      for( int i = 0; i < numGrades; i++) {
         if (categoryChar == grades[i].charAt(0)) {
            gradesNew[count] = Double.parseDouble(grades[i].substring(1));
            count++;
         }
      }
      return gradesNew;
   }
   public String toString() {
      String result = "\tStudent Name: " + getName()
         + "\n\tActivities: " + gradesByCategory('A')
         + "\n\tQuizzes: " + gradesByCategory('Q')
         + "\n\tProjects: " + gradesByCategory('P')
         + "\n\tExams: " + gradesByCategory('E')
         + "\n\tFinal Exam: " + gradesByCategory('F')
         + "\n\tCourse Average: " + courseAvg();
      return result;
   }
   public void addGrade(String newGrade) {
      if (numGrades >= grades.length) {
         increaseGradesCapacity();
      }
      grades[numGrades] = newGrade;
      numGrades++;
   }
   public boolean deleteGrade(String gradeDelete) {
      boolean delete = false;
      int deleteIndex = -1;
      for (int i = 0; i < numGrades; i++) {
         if (gradeDelete.charAt(0) == grades[i].charAt(0) && 
            Double.parseDouble(gradeDelete.substring(1)) 
            == Double.parseDouble(grades[i].substring(1))) {
            deleteIndex = i;
         }
      }   
      if (deleteIndex > -1) {
         for (int i = deleteIndex; i < numGrades - 1; i++) {
            grades[i] = grades[i + 1];
         }
         grades[numGrades - 1] = "";
         numGrades--;
         return true;
      }
      else {
         return false;
      }
   }
   public void increaseGradesCapacity() {
      String[] temporary = new String[grades.length + 1];
      for (int i = 0; i < grades.length; i++) {
         temporary[i] = grades[i];
      }
      grades = temporary;
   }
   public double average(double[] newArray) {
      if (newArray.length == 0) {
         return 0.0;
      }
      double sum = 0;
      double average = 0;
      for ( int i = 0; i < newArray.length; i++) {
         sum += newArray[i];
         average = sum / newArray.length; 
      }
      return average;
   }
   public double courseAvg() {
      double actvAvg = 0.0;
      double quizAvg = 0.0;
      double projAvg = 0.0;
      double examAvg = 0.0;
      double finalAvg = 0.0;
      double avg = 0.0; 
      if (!numGrades.length == 0) {
         avg = actvAvg * ACTV_WT + quizAvg * QUIZ_WT + projAvg * PROJ_WT + examAvg * EXAM_WT + finalAvg * FINAL_EXAM_WT;
      }   
      return avg;
   }
}

二等舱

import java.util.Scanner;
import java.io.IOException;

public class WeightedGradesApp {

   public static void main(String[] args) throws IOException {

      String name = "";
      int numGrades = 0;
      String[] grades = new String[13];
      String code =  "";
      String gradeAdd = "";
      String gradeDelete = "";
      String categoryIn = "";

      WeightedGrades student = new WeightedGrades(name, numGrades, grades);
      Scanner userInput = new Scanner(System.in);

      if (args == null) {
         System.out.println("File name was expected as a run argument.");
         System.out.println("Program ending."); 
         return;
      }
      else {
         System.out.println("File read in and WeightedGrades object created.");
         System.out.println("");
         System.out.println("Player App Menu");
         System.out.println("P - Print Report");
         System.out.println("C - Print Category");
         System.out.println("A - Add Grade");
         System.out.println("D - Delete Grade");
         System.out.println("Q - Quit ");
         do {
            System.out.print("Enter Code [P, C, A, D, or Q]: ");
            code = userInput.nextLine();
            if (code.length() == 0) {
               continue;
            }
            code = code.toUpperCase();
            char codeChar = code.charAt(0);
            switch (codeChar) {   
               case 'P':
                  System.out.println(student.toString());
                  break;
               case 'C':
                  System.out.print("      Category: ");
                  categoryIn = userInput.nextLine();
                  char categoryChar = categoryIn.charAt(0);
                  System.out.println(student.gradesByCategory(categoryChar));
                  break;
               case 'A':
                  System.out.print("      Grade to add: ");
                  gradeAdd = userInput.nextLine();
                  student.addGrade(gradeAdd);
                  break;
               case 'D':
                  System.out.print("      Grade to delete: ");
                  gradeDelete = userInput.nextLine();
                  boolean isDeleted = student.deleteGrade(gradeDelete);
                  if (isDeleted) {
                     System.out.println("      Grade deleted");
                  }
                  else {
                     System.out.println("      Grade not found");
                  }   
                  break;
               case 'Q':
                  break;      
               default:
            }
         } while (!code.equalsIgnoreCase("Q"));
      }      
   }
}
4

1 回答 1

0

对于初学者,由于该行,您的代码无法编译

if (!numGrades.length == 0) { 

这是因为numGradesint是原始类型,因此没有任何属性length。我假设你想要的是

if (numGrades != 0) {

另外,正如我提到的,您不是在处理读取文件,您提供了文件名但从未实际读取过它,我建议您查看有关 File IO 的 Java 教程

在此注释上,您进行检查args == null这将不会检查没有提供任何参数,请尝试。你想要的是args.length == 0

在您的另一个错误中,我什至不知道您是如何产生的...我假设它使用的是尚未编写方法的类的较旧编译版本。

于 2013-10-20T20:52:38.153 回答