繁体   English   中英

从Java数组列表中删除学生时出错

[英]Error when removing student from Java arraylist

我正在努力让该程序完全按照作业要求执行。 尝试删除添加的学生时,它将引发空指针异常。 同样,当我列出学生时,所有内容都显示为空。

- 代码是固定的 -

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    ArrayList<Student> newStudents = new ArrayList<Student>();


    System.out.println("Welcome to the Student Interface!.");
    System.out.println("Please select a number from the options below \n");

    while (true) {
        // Give the user a list of their options
        System.out.println("1: Add a student to the list.");
        System.out.println("2: Remove a student from the list.");
        System.out.println("3: Display all students in the list.");
        System.out.println("0: Exit the student interface.");

        // Get the user input

        int userChoice = input.nextInt();
        switch (userChoice) {
        case 1:
            addStudents(newStudents);
            break;
        case 2:
            removeStudent(newStudents);
            break;
        case 3:
            displayStudent(newStudents);
            break;
        case 0:
            System.out.println("Thank you for using the student interface. See you again soon!");
            System.exit(0);
        }
    }
}

public static void addStudents(ArrayList<Student> newStudents) {


    Scanner input = new Scanner(System.in);

   Student newStudent = new Student();

    System.out.println("Please enter first name: ");
    String First_Name = input.next();
    newStudent.setFirst_Name(First_Name);
    System.out.println("Please enter last name: ");
    String Last_Name = input.next();
    newStudent.setLast_Name(Last_Name);
    System.out.println("Please enter major: ");
    String Major = input.next();
    newStudent.setMajor(Major);
    System.out.println("Please enter GPA: ");
    String GPA = input.next();
    newStudent.setGPA(GPA);
    System.out.println("Please enter UIN: ");
    String UIN = input.next();
    newStudent.setUIN(UIN);
    System.out.println("Please enter NetID: ");
    String NetID = input.next();
    newStudent.setNetID(NetID);
    System.out.println("Please enter Age: ");
    String Age = input.next();
    newStudent.setAge(Age);
    System.out.println("Please enter Gender: ");
    String Gender = input.next();
    newStudent.setGender(Gender);



    if (newStudents.size() <= 10) { 
        newStudents.add(newStudent);

        System.out.println("Student added\n");
    } else {
        System.out.println("\n Student interface is full!");
    }

}

private static void displayStudent(ArrayList<Student> newStudents) {


    for (Student e : newStudents) {
        System.out.println(e);
    }
}

private static void removeStudent(ArrayList<Student> newStudents) {
    Scanner input = new Scanner(System.in);

    System.out.println("Please, enter the UIN to remove the Student: ");
    String uin = input.nextLine();

    for (Student e : newStudents) {
        if (e.getUIN().equals(uin)) {
            newStudents.remove(e);
            System.out.println("Student removed");
            break;
        }

        else {
            System.out.println("Sorry, no such student with this " + uin + " " + "number exist");

        }

    }

}

学生班:

包分配;

公共班学生{

private String First_Name;

private String Last_Name;

private String Major;

private String GPA;

private String UIN;

private String NetID;

private String Age;

private String Gender;

公共字符串getFirstName()

{

    return First_Name;

}

public void setFirst_Name(String value)

{

    this.First_Name = value;

}

public String getLastName()

{

    return Last_Name;

}

public void setLast_Name(String value)

{

    Last_Name = value;

}

public String getMajor()

{

    return Major;

}

public void setMajor(String value)

{

    Major = value;

}

public String getGPA()

{

    return GPA;

}

public void setGPA(String value)

{

    GPA = value;

}

public String getUIN()

{

    return UIN;

}

public void setUIN(String value)

{

    UIN = value;

}

public String getNetID()

{

    return NetID;

}

public void setNetID(String value)

{

    NetID = value;

}

public String getAge()

{

    return Age;

}

public void setAge(String value)

{

    Age = value;

}

public String getGender()

{

    return Gender;

}

public void setGender(String value)

{

    Gender = value;

}

public String toString()

{

    return "First Name: " + First_Name +

            "\n Last Name: " + Last_Name +

            "\n Major: " + Major +

            "\n GPA: " +GPA+

            "\n UIN: " + UIN +

            "\n NetID: " + NetID+

            "\n Age: " + Age+

            "\n Gender: " + Gender;

}

  public void createStudent(String first_Name2, String last_Name2, String major2, String gPA2, String uIN2, String netID2,

              String age2, String gender2) {



      first_Name2 = First_Name;

      last_Name2 = Last_Name;

      major2 = Major;

      gPA2 = GPA;

      uIN2 = UIN;

      age2 = Age;

      gender2 = Gender;



  }

}

您的程序可以在我的计算机上完美运行。 这是一个示例运行:

Welcome to the Student Interface!.
Please select a number from the options below 

1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
2
Please, enter the UIN to remove the Student: 
13
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
1
Please enter first name: 
o
Please enter last name: 
v
Please enter major: 
cs
Please enter GPA: 
g
Please enter UIN: 
79
Please enter NetID: 
o
Please enter Age: 
57
Please enter Gender: 
m
Student added

1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
3
Student [firstName=o, lastName=v, major=cs, gPA=g, uIN=79, netID=o, age=57, gender=m]
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
2
Please, enter the UIN to remove the Student: 
79
Student removed
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
3
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
0
Thank you for using the student interface. See you again soon!

程序中的潜在问题包括:您在System.in上有两个Scanner对象,您可能只想共享一个。 您不使用变量student_added 在我的两个案例中,程序没有向用户报告是否有学生被遣散。 您的两个TODO注释已过时,应删除。

remove操作上收到NullPointerException的两个原因之一。 ArrayList为null,或者您尝试删除的对象为null。 检查那些。

暂无
暂无

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

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