2016-12-15 76 views
1

我想將這些數據從文件中讀取到我的地圖中。我的地圖是一張樹圖TreeMap<String, Student>,其中學生在另一班。我正在嘗試使用代碼map.put(formatSNumber, student.setCourses(courses));將讀取的文件元素添加到我的地圖中,但我一直遇到void type not permitted here錯誤。虛空類型在這裏不允許出錯

sNumber = Integer.parseInt(Breader.readLine()); 
formatSNumber = String.format("%03d", sNumber); 
hours = Integer.parseInt(Breader.readLine()); 
grade = Double.parseDouble(Breader.readLine()); 
Student student = map.get(formatSNumber); 
Course course = new Course(hours, grade); 
List<Course> courses = student.getCourses(); 
courses.add(course); 
map.put(formatSNumber, student.setCourses(courses)); 
end = Breader.ready(); 

這裏是我的全碼:

import java.io.*; 
import java.util.Scanner; 
import java.util.TreeMap; 
import java.util.Iterator; 
import java.util.List; 
public class FinalProgram { 
    public static void main(String[] args) throws IOException { 
     String nameFile = " "; 
     String classFile = " "; 
     TreeMap<String, Student> map = new TreeMap<>(); 
     Scanner input = new Scanner(System.in); 

     try { 
      System.out.print("Enter the Name file(c:filename.txt): "); 
      nameFile = input.nextLine(); 
     } catch(IllegalArgumentException e) { 
      System.out.printf("Invalid input. Please enter" 
        + " filename in the form of " 
        + "c:filename.txt\n", e.getMessage()); 
     } 
     nameReader(nameFile, map); 

     try { 
      System.out.print("Enter the Class file(c:filename.txt): "); 
      classFile = input.nextLine(); 
     } catch(IllegalArgumentException e) { 
      System.out.printf("Invalid input. Please enter" 
        + " filename in the form of " 
        + "c:filename.txt\n", e.getMessage()); 
     } 
     classReader(classFile, map); 
    } 

    private static void nameReader(String file, TreeMap<String, Student> map) 
      throws IOException { 

     String nameFile = file; 
     int sNumber = 0; 
     String formatSNumber = " "; 
     String sName = " "; 

     //Instantiate FileReader and BufferedReader 
     FileReader freader = new FileReader(nameFile); 
     BufferedReader Breader = new BufferedReader(freader); 
     boolean end = Breader.ready(); 

     do { 
      sNumber = Integer.parseInt(Breader.readLine()); 
      formatSNumber = String.format("%03d", sNumber); 
      sName = Breader.readLine(); 
      Student student = new Student(sName); 
      map.put(formatSNumber, student); 
      end = Breader.ready(); 
     } while(end); 
     Iterator<String> keySetIterator = map.keySet().iterator(); 
     while(keySetIterator.hasNext()) { 
      String key = keySetIterator.next(); 
      System.out.println("key: " + key + " value: " + map.get(key).getName()); 
     } 
    } 
    private static void classReader(String file, TreeMap<String, Student> map) 
      throws IOException { 
     String classFile = file; 
     int sNumber = 0; 
     String formatSNumber = " "; 
     int hours = 0; 
     double grade = 0.0; 
     double points = grade * hours; 
     double GPA = points/hours; 

     //Instantiate FileReader and BufferedReader 
     FileReader freader = new FileReader(classFile); 
     BufferedReader Breader = new BufferedReader(freader); 
     boolean end = Breader.ready(); 

     do { 
      sNumber = Integer.parseInt(Breader.readLine()); 
      formatSNumber = String.format("%03d", sNumber); 
      hours = Integer.parseInt(Breader.readLine()); 
      grade = Double.parseDouble(Breader.readLine()); 
      Student student = map.get(formatSNumber); 
      Course course = new Course(hours, grade); 
      List<Course> courses = student.getCourses(); 
      courses.add(course); 
      map.put(formatSNumber, student.setCourses(courses)); 
      end = Breader.ready(); 
     } while(end); 

     points = grade * hours; 
     GPA = points/hours; 
    } 
} 

Student類:

import java.util.ArrayList; 
import java.util.List; 
public class Student { 
    private String name = " "; 
    private List<Course> courses = new ArrayList<>(); 
    public Student(String name) { 
     this.name = name; 
    } 
    public Student(String name, List courses) { 
     this.name = name; 
     this.courses = courses; 
    } 
    public List getCourses() { 
     return courses; 
    } 
    public void setCourses(List courses) { 
     this.courses = courses; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getName() { 
     return name; 
    } 
} 

課程類:

public class Course { 
    private int hours = 0; 
    private double grade = 0.0; 
    public Course(int hours, double grade) { 
     this.hours = hours; 
     this.grade = grade; 
    } 
    public void setHours(int hours) { 
     this.hours = hours; 
    } 
    public int getHours() { 
     return hours; 
    } 
    public void setGrade(double grade) { 
     this.grade = grade; 
    } 
    public double getGrade() { 
     return grade; 
    } 
} 
+0

student.getCourses(courses)返回類型列表。然而,地圖要求學生作爲一種價值。 – Stefan

回答

2

的想法把約爲東西到該地圖。

更準確地說,您通常會提供(非空)鍵和值對象。

您正在使用student.setCourses(courses)作爲put()預期的那個「值」參數的參數。

該參數是表達式。該表達式的結果將是方法調用的結果。

該方法被定義爲不返回什麼(無效)。

顯然沒有是不一樣的東西。這就是編譯器試圖告訴你的。

兩個解決方案:

  1. 傳遞一個Student對象
  2. 改變這個方法setCourses()

像這樣:

Student setCourses(... { 
.... 
    return this; 
} 

(你更好的去選擇1; 2更多是一種骯髒的黑客攻擊,實質上是不好的做法)

3

map.put(formatSNumber, student.setCourses(courses))第二個參數的類型必須爲學生的。 student.setCourses(courses)是一個返回類型爲void的setter方法,即不返回。這不匹配。

例如,您必須有類似map.put("someString", new Student("name"))的東西,或者map.put("someString", student),其中student的類型爲Student

+0

好的。我感覺有點不知所措。我正在嘗試將'Course'對象添加到'Student'對象集合的課程中,並且我迷失在這裏的語法中。 –

+0

我不確定這是否是您想要的,但請嘗試:列表 courses = student.getCourses(); courses.add(course); map.put(formatSNumber,student);你也可以這樣做:student.getCourses()。add(course); map.put(formatSNumber,student)。這是相同的。 – Stefan

+0

我怎樣才能深入地圖並訪問'學生'? 我想像這樣進入地圖: 拉起studentID(formatSNumber),然後顯示相應的'student'對象的課程。 –