简体   繁体   中英

Reading from keyboard to build a relationship between objects

I want to create an object named "Course", and get the information from the keyboard. The last attribute called the "pre", which means the prerequisite courses of this course. I want to input the whole information in one line and extract the information for each attribute. But I got the problem with"pre". I run the program and the output of course.pre is null. I do not know why. Here is my Course class code: `import java.util.HashSet;

public class Course{

private String name;
private int isFall;
private int NumPre;
private HashSet<Course> pre;

public Course(String name) {
    this.name = name;
}

public String getName() {
    return name;
}
public String setName (String n){
    return name = n;
}

// 1 - fall 0 - both   -1 - spring
public void setType(String isFall) {
    if(isFall.equals("F") || isFall.equals("f")){
        this.isFall = 1;
    }else if(isFall.equals("S") || isFall.equals("s")){
        this.isFall = -1;
    }else if(isFall.equals("B") || isFall.equals("b")){
        this.isFall = 0;
    }
}
public int getType(){
    return isFall;
}  

public void SetNumPre(int n) {
    this.NumPre = n;
}
public int getNumPre() {
    return NumPre;
}

public void addPre(Course c) {
    pre.add(c);
}
public HashSet<Course> getPre() {
    return pre;
}

} ` And here is my main method here:

import java.util.*;

public class TimeToGraduate {

public static void main(String[] args){

    Scanner scanner = new Scanner(System.in);
    //System.out.print("Input first two integers here: ");
    String globalInfo = scanner.nextLine();
    String[] numOfCourse = globalInfo.split(" ");//[0] num of total course  [1] max num per semester
    int totalNum = Integer.parseInt(numOfCourse[0]);
    int maxPre = Integer.parseInt(numOfCourse[1]);

    Course courses[] = new Course[totalNum];        
    //System.out.print("Please input course list here: ");
    String coursesList = scanner.nextLine();
    String[] nameOfCourse = coursesList.split(" ");
    for(int i = 0;i < totalNum; i++){
        courses[i] = new Course(nameOfCourse[i]);
    }

    //System.out.print("Please input course info here: ");
    for(int i = 0;i < totalNum; i++){
        String courseInfo = scanner.nextLine();
        String[] infoOfCourse = courseInfo.split(" ");
        courses[i].setName(infoOfCourse[0]);
        courses[i].setType(infoOfCourse[1]);
        courses[i].SetNumPre(Integer.parseInt(infoOfCourse[2]));
        if(courses[i].getNumPre() > 0){
            for(int j = 3; j < 3+(courses[i].getNumPre()); j++){
                for(int k = 0; k < totalNum; k++){
                    if(infoOfCourse[j] == courses[k].getName()){
                        courses[i].addPre(courses[k]);                          
                    }                           
                }
            }               
        }
    }



    scanner.close();
    for(int m = 0; m < totalNum; m++){
        System.out.print(courses[m].getName()+" ");
        System.out.print(courses[m].getType()+" ");
        System.out.print(courses[m].getNumPre()+" ");
        System.out.print(courses[m].getPre()+" ");
        System.out.println();
    }
}

}

Notice that you did not initilize the pre attribute. That is why it is null.

It would be a good practise if you initilize the pre inside a constructor for the Course class. Otherwise, do it when you start filling the Course attributes.

Update:

Your constructor should be like this:

 public Course() { this.pre = new  HashSet()} 

As you can see the constructor does not have any arguements, because you will be filling its attribute from the main function.

You can define a constructor with arguments too:

       public Course(String name, HashSet<Course> pre) 
       { this.name = name; this.pre = pre; }

But you will need to initilize pre and name when you call it from the main:

  ...
 HashSet hs = new HashSet();
 course[i] = new Course('course_name', hs);
  ....

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