我正在努力让这个程序完全按照作业的要求去做。当试图删除一个添加的学生时,它抛出一个空指针异常。另外,当我列出学生时,它显示所有内容都是空的。
-代码是固定的-
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;
}
}
在remove
操作上会出现NullPointerException
的两个原因之一。ArrayList
为空,或者您试图删除的对象为空。检查这些。
你的程序在我的电脑上运行得非常好。下面是一个运行示例:
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
上有两个扫描仪
对象,您可能只想共享一个。不要使用变量student_added
。在我的两个案例中,程序没有向用户报告学生是否被删除。您的两条 TODO 注释已过时,应将其删除。
问题内容: 对此问题的任何帮助将不胜感激。 我有一个元组列表 并且我需要删除某种类型的重复项:根据我的定义,(1,2)和(2,1)被视为重复项。所需输出 提前致谢 问题答案: 您可以对它们进行排序,然后使用删除重复项:
我有一个arraylist的arraylist,每个arraylist都有相同的点,例如,当您打印时,这是输出 在打印之前,我要删除其x值不等于数组列表索引的所有点 所以输出应该是 我尝试使用remove方法和for循环以及其他方法,但都不起作用。 有人能帮忙吗?
我试图从ArrayList中删除所有奇数,并且它必须是for或Fore每循环。循环结束后,ArrayList中剩余的数字的结果将7, 90, 55, 60。当我设定一个条件时: 一切工作正常。所有偶数都被删除,但在第一个示例中,奇数的情况并非如此。为什么会发生这种情况?
我试图将ArrayList的内容保存到一个txt文件中,但当我这样做时,会得到很多不需要的字符。逗号是我可以处理字符串的。replace()但我想知道是否有其他方法可以更有效地过滤这些类型的字符?尤其是“,”t[”txt文件中的一部分。上面的引号中还显示了其他字符,但我不想在这里复制它们。我要输出到文件的代码如下。我正在向ArrayList添加几个字符串,但问题似乎在于ArrayList本身。如下
目前,我创建了单独的,最后连接这些,以创建一个。 是否有优雅的或一个衬里来从数组列表和多个数组中创建df。
问题内容: 有什么快速(好看的)方法可以从Java中的数组中删除元素? 问题答案: 你可以使用的。