2016-10-11 8 views
1

私のタイトルは理にかなっていることを望みます。私のコードで何をしようとしているのかは、学生(学生ID、名、姓など)と彼らが取ったコースについての属性を持つファイルごとに読み込まれます。私はバッファリングされた読者が大丈夫だと確信していますが、私のプログラムの仕様については少し失われています。この問題は、CourseList ArrayListオブジェクトをStudent ArrayListオブジェクトに追加するときに発生します。コードはうまく動作しますが、コースリストを生徒オブジェクトに追加しません。基本的に私は2つのStudent ArrayListsを作成しており、それらに関連してコースを保存する必要があります。すなわち)STUDENT1、Course1、Course2、など。別のオブジェクトタイプの既存のArrayListにArrayListオブジェクトを追加する

はここで、事前にありがとうは、私のコードは次のとおりです。

import java.io.*; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.StringTokenizer; 

public class studentDir { 
    static int currentStudent = 0; 
    public static void main(String args[]) throws IOException{ 
     ArrayList<Student> students = new ArrayList<Student>(); 

     Scanner input = new Scanner(new File("WarmUpData.txt")); 

     while(input.hasNextLine()) { 

      String line = input.nextLine(); 
      StringTokenizer st = new StringTokenizer(line,","); 

       while(st.hasMoreTokens()){ 
        String first, last, idNo; 
        last = st.nextToken(); 
        first = st.nextToken(); 
        System.out.println("Added student: "+last+", "+first); 
        idNo = st.nextToken(); 
        System.out.println("Stored ID: "+idNo); 
        Student s = new Student(last, first, idNo); 
        students.add(s); 
        line = input.nextLine(); 
        st = new StringTokenizer(line,","); 
        while(st.hasMoreTokens()){ 
         String x = st.nextToken(); 
         System.out.println("If controller read in: "+x); 
         if(x.equals("-999")){ 
          line = input.nextLine(); 
          st = new StringTokenizer(line, ","); 
          System.out.println("Added Credits & GPA"); 
          Float totalCredits = Float.parseFloat(st.nextToken()); 
          Float gpa = Float.parseFloat(st.nextToken()); 
          students.get(currentStudent).storeGPA(totalCredits, gpa); 
          System.out.println("GPA Read In : "+students.get(currentStudent).getGPA()); 
          currentStudent++; 
         } 
         else{ 
          System.out.println("Adding course."); 
          String courseID = x; 
          float credits = Float.parseFloat(st.nextToken()); 
          String grade = st.nextToken(); 
          System.out.println(x+", "+grade); 
          CourseList cl = new CourseList(courseID,grade,credits); 
          s.add(cl); 
          System.out.println(cl.toString()); 
          System.out.println(courseID+" "+credits+" "+grade+" added."); 
          line = input.nextLine(); 
          st = new StringTokenizer(line,","); 
          } 
         } 

        for(Student x : students) 
         System.out.println(x.toString());      
       } 

     } 
     input.close();  
     currentStudent = 0; 
    } 
} 

import java.util.ArrayList; 

public class Student { 
    private String firstName; 
    private String lastName; 
    private String studentID; 
    private float gpa; 
    private float totalCredits; 
    private ArrayList<CourseList> courses = new ArrayList<>(); 

    Student(String y, String x, String z){ 
      this.firstName = x; 
      this.lastName = y; 
      this.studentID = z; 
    } 

     public String toString(){ 
      String x = (this.firstName+" "+this.lastName+" "+this.studentID+"."); 
      return x; 
     } 
     public void setGPA(float x){ 
      this.gpa = x; 
     } 

     public float getGPA(){ 
      return gpa; 
     } 

     public String getID(){ 
      return this.studentID; 
     } 

     public void gpaCalc(Student stu, String id){ 
      totalCredits = 0; 

     } 

     public void storeGPA(float tcredits, float gpa){ 
      this.gpa = gpa; 
      this.totalCredits = tcredits; 
     } 
     public void add(CourseList cl) { 

     } 

} 

public class CourseList { 
    String idNo, grade, courseID; 
    float credits; 
    float gpa; 

    public CourseList(String x, String y, float z) { 
     this.courseID = x; 
     this.grade = y; 
     this.credits = z; 
    } 

    public String toString(){ 
     String x = ("Course ID: "+this.courseID+", Grade : "+this.grade+", Credits Earned : "+this.credits+"."); 
     return x; 
    } 

    public float getCredits() { 
     return this.credits; 
    } 
} 


Input: 
Jones,Mary,903452 
4342,2.5,A 
3311,4,B+ 
-999 
6.5,3.569 
Martin,Joseph,312345 
4598,3,C 
1122,3,A- 
2467,4,A 
-999 
10,3.31 

Output: 
Added student: Jones, Mary 
Stored ID: 903452 
If controller read in: 4342 
Adding course. 
4342, A 
Course ID: 4342, Grade : A, Credits Earned : 2.5. 
4342 2.5 A added. 
If controller read in: 3311 
Adding course. 
3311, B+ 
Course ID: 3311, Grade : B+, Credits Earned : 4.0. 
3311 4.0 B+ added. 
If controller read in: -999 
Added Credits & GPA 
GPA Read In : 3.569 
Mary Jones 903452. 
Added student: Martin, Joseph 
Stored ID: 312345 
If controller read in: 4598 
Adding course. 
4598, C 
Course ID: 4598, Grade : C, Credits Earned : 3.0. 
4598 3.0 C added. 
If controller read in: 1122 
Adding course. 
1122, A- 
Course ID: 1122, Grade : A-, Credits Earned : 3.0. 
1122 3.0 A- added. 
If controller read in: 2467 
Adding course. 
2467, A 
Course ID: 2467, Grade : A, Credits Earned : 4.0. 
2467 4.0 A added. 
If controller read in: -999 
Added Credits & GPA 
GPA Read In : 3.31 
Mary Jones 903452. 
Joseph Martin 312345. 

は、事前にみんなにありがとう!あなたのコードで

+0

学生#は方法は、あなたがHashMapを使用して考えがあり – user3662708

+0

何もしない追加動作します学生のクラスにCourseListのArrayListの設定? –

+0

私はすべてのurコードを通過しませんでしたが、while(st.hasMoreTokens())は、このループが最初の学生で2回繰り返されると、学生1人しかいない間にstudents.get(1)あなたの現在の学生を追跡している方法の問題を再考する –

答えて

0

は、あなたのaddCourse方法は

 public void add(CourseList cl) { 

    } 

私はここで良いアイデアではありません二つの異なるタイプのArrayListのを使用して、ここで

+0

この方法をプログラムする方法に関するアイデアはありますか?私は完全にarrayListsに精通していない。 – xfbim

+0

@xfbim https://www.tutorialspoint.com/java/util/arraylist_add_index.htm これは、ArrayListに要素を追加する方法を理解するのに役立ちます – Nordiii

1

私見あなたの問題だと思い空で切り取ら。 Map<Student, ArrayList<Course>>のようなものを使用しているはずです。それは:

  1. 人間が読めるように維持することができます。
  2. タイプセーフです。
  3. O(1)で学習するすべてのコースを検索するような操作を可能にします。
+0

プログラムの仕様には残念なことにリンクリストまたは配列リストが必要ですが、私は将来、インストラクターがより堅牢なデータタイプと検索方法を使用するように求めると思います。 – xfbim

0

ちょうどその時、それは

public class studentDir { 
       static int currentStudent = 0; 
       public static void main(String args[]) throws IOException{ 
        ArrayList<Student> students = new ArrayList<Student>(); 
    ArrayList<CourseList> courseLists=new ArrayList<>(); 
        Scanner input = new Scanner(new File("WarmUpData.txt")); 

        while(input.hasNextLine()) { 

         String line = input.nextLine(); 
         StringTokenizer st = new StringTokenizer(line,","); 

          while(st.hasMoreTokens()){ 
           String first, last, idNo; 
           last = st.nextToken(); 
           first = st.nextToken(); 
           System.out.println("Added student: "+last+", "+first); 
           idNo = st.nextToken(); 
           System.out.println("Stored ID: "+idNo); 
           Student s = new Student(last, first, idNo); 

           line = input.nextLine(); 
           st = new StringTokenizer(line,","); 
           while(st.hasMoreTokens()){ 
            String x = st.nextToken(); 
            System.out.println("If controller read in: "+x); 
            if(x.equals("-999")){ 
             line = input.nextLine(); 
             st = new StringTokenizer(line, ","); 
             System.out.println("Added Credits & GPA"); 
             Float totalCredits = Float.parseFloat(st.nextToken()); 
             Float gpa = Float.parseFloat(st.nextToken()); 
             students.get(currentStudent).storeGPA(totalCredits, gpa); 
             System.out.println("GPA Read In : "+students.get(currentStudent).getGPA()); 
             currentStudent++; 
            } 
            else{ 
             System.out.println("Adding course."); 
             String courseID = x; 

             float credits = Float.parseFloat(st.nextToken()); 
             String grade = st.nextToken(); 
             System.out.println(x+", "+grade); 
             CourseList cl = new CourseList(courseID,grade,credits); 
             courseLists.add(cl); 
             s.setCourses(courseLists); 
             System.out.println(cl.toString()); 
             System.out.println(courseID+" "+credits+" "+grade+" added."); 
             line = input.nextLine(); 
             st = new StringTokenizer(line,","); 

            } 
            students.add(s); 

            } 

           for(Student x : students) 
            System.out.println(x);      
          } 

        } 
        input.close();  
        currentStudent = 0; 
       } 
      } 



    class Student { 
     private String firstName; 
     private String lastName; 
     private String studentID; 
     private float gpa; 
     private float totalCredits; 
     private ArrayList<CourseList> courses = new ArrayList<>(); 

     Student(String y, String x, String z) { 
      this.firstName = x; 
      this.lastName = y; 
      this.studentID = z; 
     } 

     public String toString() { 
      String x = (this.firstName + " " + this.lastName + " " + this.studentID + "."); 
      return x; 
     } 

     public void setGPA(float x) { 
      this.gpa = x; 
     } 

     public float getGPA() { 
      return gpa; 
     } 

     public String getID() { 
      return this.studentID; 
     } 

     public void gpaCalc(Student stu, String id) { 
      totalCredits = 0; 

     } 

     public void storeGPA(float tcredits, float gpa) { 
      this.gpa = gpa; 
      this.totalCredits = tcredits; 
     } 



     public ArrayList<CourseList> getCourses() { 
      return courses; 
     } 

     public void setCourses(ArrayList<CourseList> courses) { 
      this.courses = courses; 
     } 

    } 

    class CourseList { 
     String idNo, grade, courseID; 
     float credits; 
     float gpa; 

     public CourseList(String x, String y, float z) { 
      this.courseID = x; 
      this.grade = y; 
      this.credits = z; 
     } 

     public String toString() { 
      String x = ("Course ID: " + this.courseID + ", Grade : " + this.grade + ", Credits Earned : " + this.credits 
        + "."); 
      return x; 
     } 

     public float getCredits() { 
      return this.credits; 
     } 
    } 
関連する問題