Java 添加值后返回null的HashMap

Java 添加值后返回null的HashMap,java,hashmap,Java,Hashmap,我知道以前有人问过这个问题,但我不明白如何解决这个问题。我将值添加到HashMap和setStudent中,然后尝试在replaceName中更改值,但键似乎不存在 import java.util.*; public class Student { private HashMap<String, List<String>> studentDetails = new HashMap<String, List<String>>();

我知道以前有人问过这个问题,但我不明白如何解决这个问题。我将值添加到HashMap和setStudent中,然后尝试在replaceName中更改值,但键似乎不存在

import java.util.*;

public class Student {

    private HashMap<String, List<String>> studentDetails = new HashMap<String, List<String>>();
    private HashMap<String, List<String>> studentModules = new HashMap<String, List<String>>();

    public void setStudent(String id, String name, String postalAddress, String emailAddress, String degreeTitle, String dateEnrolled, List<String> modules) {
        List<String> information = new ArrayList<>();
        information.add(name);
        information.add(postalAddress);
        information.add(emailAddress);
        information.add(degreeTitle);
        information.add(dateEnrolled);
        studentDetails.put(id, information);
        studentModules.put(id, modules);
    }

    public List<String> getStudentDetails(String x) {
        return studentDetails.get(x);
    }

    public List<String> getStudentModules(String x) {
        return studentModules.get(x);
    }

    public void replaceName(String id, String name, String postalAddress, String emailAddress, String degreeTitle, String dateEnrolled) {

        List<String> information = new ArrayList<>();
        information.add(name);
        information.add(postalAddress);
        information.add(emailAddress);
        information.add(degreeTitle);
        information.add(dateEnrolled);
        studentDetails.replace(id, information);
    }
}
import java.util.*;
公立班学生{
private HashMap studentDetails=new HashMap();
私有HashMap studentModules=newhashmap();
public void setStudent(字符串id、字符串名称、字符串PostLaddress、字符串emailAddress、字符串degreeTitle、字符串DateRegistered、列表模块){
列表信息=新建ArrayList();
信息。添加(名称);
信息。添加(邮资);
信息。添加(电子邮件地址);
信息。添加(degreeTitle);
信息。添加(日期登记);
学生详细信息。输入(id、信息);
studentModules.put(id,modules);
}
公共列表getStudentDetails(字符串x){
返回studentDetails.get(x);
}
公共列表GetStudentModule(字符串x){
返回studentModules.get(x);
}
public void replaceName(字符串id、字符串名称、字符串postalAddress、字符串emailAddress、字符串degreeTitle、字符串dateEnrolled){
列表信息=新建ArrayList();
信息。添加(名称);
信息。添加(邮资);
信息。添加(电子邮件地址);
信息。添加(degreeTitle);
信息。添加(日期登记);
学生详细信息。替换(id、信息);
}
}
我的主类相当长,但基本上需要用户输入才能获得值:

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner source = new Scanner(System.in);
        RecordManager r = new RecordManager();
        Module m = new Module();
        Student s = new Student();
        ChangeName c = new ChangeName();

        while (true) {
            System.out.println("Please enter the student id:");
            r.setId(source.nextLine());

            System.out.println("Please enter the student name:");
            r.setName(source.nextLine());

            System.out.println("Please enter the student address:");
            r.setAddress(source.nextLine());

            System.out.println("Please enter the student email address:");
            r.setEmailAddress(source.nextLine());

            System.out.println("Please enter the degree title:");
            r.setDegreeTitle(source.nextLine());

            System.out.println("Please enter the date enrolled:");
            r.setDateEnrolled(source.nextLine());

            List<String> a = new ArrayList<>();
            while (true) {
                System.out.println("Please enter the module name:");
                String name = source.nextLine();
                m.setName(name);
                a = r.addModule(name, a);
                m.setTitle(name);

                System.out.println("Please enter the module code:");
                m.setCode(source.nextLine());

                while (true) {
                    System.out.println("Please enter the module mark (between 1 and 100):");
                    int mark = source.nextInt();
                    if (mark > 0 && mark <101) {
                        m.setMark(mark);
                        break;
                    }
                    else {
                        System.out.println("Module mark must be between 1 and 100");
                    }
                }

                System.out.println("Please enter the module credits:");
                m.setCredits(source.nextInt());

                m.setModule();

                System.out.println("Would you like to enter another module? (Y/N)");
                String more = source.next();
                if (more.equalsIgnoreCase("N")) {
                    break;
                }
                source.nextLine();
            }
            r.setModules(a);
            s.setStudent(r.getId(), r.getName(), r.getAddress(), r.getEmailAddress(), r.getDegreeTitle(), r.getDateEnrolled(), r.getModules());     
            r.addStudents(r.getId(), s.getStudentDetails(r.getId()));

            System.out.println("Would you like to enter the details for another student? (Y/N)");
            String another = source.next();
            if (another.equalsIgnoreCase("N")) {
                break;
            }
            source.nextLine();
        }
        while (true) {
            System.out.println("Full list of students and details (1) \nLookup a student by name (2) \nChange student name (3)");
            source.nextLine();
            int choice = source.nextInt();
            if (choice == 1) {
                for (String id : r.getStudents().keySet()) {
                    System.out.println(s.getStudentDetails(id) + " " + s.getStudentModules(id));
                }
                break;
            }
            else if (choice == 2) {
                System.out.println("Please enter the full name of the student:");
                source.nextLine();
                String name = source.nextLine();
                boolean exists = false;
                int i = 0;
                for (String id : r.getStudents().keySet()) {
                    if (s.getStudentDetails(id).get(i).equalsIgnoreCase(name)) {
                        System.out.println(s.getStudentDetails(id) + " " + s.getStudentModules(id));
                        exists = true;
                    }
                    i += 1;     
                }
                if (exists == false) {
                    System.out.println("Student not found");
                }
                break;
            }
            else if (choice == 3) {
                System.out.println("Please enter the ID of the student:");
                source.nextLine();
                String id = source.nextLine();
                System.out.println("Please enter the new name:");
                String name = source.nextLine();
                c.changeName(id, name, s.getStudentDetails(id));
                System.out.println(s.getStudentDetails(id));
                break;
            }
            else {
                System.out.println("Incorrect selection. Please enter 1, 2 or 3");
            }               
        }       
    }
}
import java.util.*;
公共班机{
公共静态void main(字符串[]args){
扫描仪源=新扫描仪(System.in);
RecordManager r=新的RecordManager();
模块m=新模块();
学生s=新学生();
ChangeName c=新的ChangeName();
while(true){
System.out.println(“请输入学生id:”);
r、 setId(source.nextLine());
System.out.println(“请输入学生姓名:”);
r、 setName(source.nextLine());
System.out.println(“请输入学生地址:”);
r、 setAddress(source.nextLine());
System.out.println(“请输入学生电子邮件地址:”);
r、 setEmailAddress(source.nextLine());
System.out.println(“请输入学位名称:”);
r、 setDegreeTitle(source.nextLine());
System.out.println(“请输入注册日期:”);
r、 setDateEnrolled(source.nextLine());
列表a=新的ArrayList();
while(true){
System.out.println(“请输入模块名称:”);
字符串名称=source.nextLine();
m、 集合名(名称);
a=r.addModule(名称,a);
m、 片名(姓名);
System.out.println(“请输入模块代码:”);
m、 setCode(source.nextLine());
while(true){
System.out.println(“请输入模块标记(介于1和100之间):”;
int mark=source.nextInt();

如果(mark>0&&mark您如何使用
Student
class?您是否可以编辑此内容以包含主方法或使用此Student类的任何代码?虽然很长,但我现在已经添加了它,谢谢:)为了帮助您考虑提交一个没有输入提示的修改类,但是用适当的值填充了一个数据结构。OK,所以我看到问题是您的CeaNeNAME类。将所有数据传递到哈希映射。为了实现这一点,您必须将您在主方法中初始化的学生类作为构造函数参数传递到ChangeName类中。注意:这不是实现此操作的最佳方法,但我只是想帮助您使用现有的方法来实现此目的。
import java.util.*;

public class ChangeName {

    RecordManager r = new RecordManager();
    Student s = new Student();

    public void changeName(String id, String newName, List<String> details) {
        s.replaceName(id, newName, details.get(1), details.get(2), details.get(3), details.get(4)); 
    }   

}