简体   繁体   English

将String ArrayList传递给返回双精度值的方法

[英]Passing a String ArrayList to a method returning a double value

I'm working on an assignment question, which im stuck at: 我正在研究一个分配问题,该问题停留在:

i extracted a text file grades.txt to an arraylist called studentgrade i want to then using the grades ive extracted to the arraylist to method convertgrade. 我想将文本文件grades.txt提取到名为studentgrade的数组列表中,然后使用提取到数组列表的ive转换为convertgrade方法。

the purpose of the convert grade is the read the line of the text file and then convert the string value to points; 转换等级的目的是读取文本文件的行,然后将字符串值转换为点; and then adding up all the grades the summed points. 然后将所有成绩的总和相加。

package Q2;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.List;

public class studentGPA {

private static String studentGrades[];

public static void main(String[] args) throws IOException {
    double gpa, points = 0;     
    //opening grades and extracting to array

    File file = new File ("Grades.txt");
    Scanner inputFile = new Scanner(file);

    ArrayList<String> studentlist = new ArrayList<String>();

            while (inputFile.hasNext())
            {
                studentlist.add(inputFile.nextLine()); 
            }

/*print out student ID

    for (int index = 0; index<studentlist.size(); index++)
    {
        StudentID = (studentlist.get(index)).split(" ",11);
        System.out.println("studentID:" + index + ": " + StudentID[0]);

    } 
    System.out.println(); */



            studentGrades = null;

    for (int index = 0; index<studentlist.size(); index++)
    {
        String data[] = studentlist.get(index).split(" ");
       for (int index_grade = 0 ; index_grade < 8;)
        {
          studentGrades[index_grade] = data[index_grade+3];
      }
        System.out.println(convertGrade(studentGrades));

    }

    //testing purposes
    /*for (int index = 0; index<studentlist.size(); index++)
    {
        StudentGrade = (studentlist.get(index)).split(" ",11);


        System.out.println("studentID:" + index + ": " + StudentGrade[3] +  StudentGrade[4] + 
                StudentGrade[5] + StudentGrade[6] + StudentGrade[7] + StudentGrade[8] + StudentGrade[9] );
    }*/




    //answer should be 46 but im getting 14


}

 public static double convertGrade(String studentgrade[]) {
      double points = 0;

      for (int index = 3; index<studentgrade.length; index++)
      {
      if (studentgrade[index].contains("H"))
          points = 7;
      else if (studentgrade[index].contains("D"))
          points = 6;
      else if (studentgrade[index].contains("C"))
          points = 5;
      else if (studentgrade[index].contains("P"))
          points = 4;
      else if (studentgrade[index].contains("F"))
          points = 0;
      points = points + points;
      }
      return points;
   }


}
  • 57363 Joy Ryder DDCPHHCD ==== 6 + 6 +5 +4 + 7 + 7 +5 + 6 = 46 57363乔伊·莱德DDCPHHCD ==== 6 + 6 +5 +4 + 7 + 7 +5 + 6 = 46
  • 72992 Laura Norder HHHDDHHH 72992劳拉·诺德(Laura Norder)HHHDDHHH
    • 71258 Eileen Over CFCDCCCP 71258艾琳超过CFCDCCCP

EDIT: 编辑:

you must do it like this : 您必须这样做:

System.out.println(convertGrade(StudentGrade));

EDIT 编辑

for (int index = 0; index<studentlist.size(); index++)
{
    String data[] = studentlist.get(index).split(" ");
    for (int index_grade = 0 ; index_grade < 8)
    {
       StudentGrade[index_grade] = data[index_grade+3];
    }
    System.out.println(convertGrade(StudentGrade));
}

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

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