如何从文件中读取行并将行分配给数组?

时间:2013-10-20 20:20:33

标签: java arrays file

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

我遇到的一些错误:

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

    WeightedGrades类:

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

1 个答案:

答案 0 :(得分:0)

对于初学者来说,由于行

,你的代码不能编译
if (!numGrades.length == 0) { 

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

if (numGrades != 0) {

另外正如我所提到的,你没有处理文件中的阅读,你提供了文件名但从未真正阅读过,我建议你看一下Java tutorial on File IO

在这个注释中,你检查args == null这不会检查没有提供args,试试看。你想要的是args.length == 0

关于你的另一个错误,我不知道你是如何制作的...我假设它使用的是没有编写方法的类的旧编译版本。