简体   繁体   中英

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.

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
  • 72992 Laura Norder HHHDDHHH
    • 71258 Eileen Over 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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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