user693389
user693389

Reputation: 21

HashMap with int array (int[]) as value returns null on get?

I have a Hashmap that stores a student name as the key and an int array of scores as the value. I know its creating the HashMap correctly but when trying to return the int array for a key I cant seem to get.

public int[] getQuizzes(String studentName)
    {
        int[] studentsQuizzes = quizMarks.get(studentName);
        return studentsQuizzes; 
    }

It just ends up returning null. What am I missing, thanks for any help

This is how I am creating the hashmap

quizMarks = new HashMap<String, int[]>();   

public void addStudent(String studentName)
    {
        String formattedName = formatName(studentName);
        int[] quizzes = new int[NUM_QUIZZES];
        for (int i = 0; i < quizzes.length; i++)
        {
            quizzes[i] = MIN_GRADE;
        }
        quizMarks.put(formattedName, quizzes);
    }

Upvotes: 0

Views: 2555

Answers (1)

ColinD
ColinD

Reputation: 110054

Your keys in the map are the results of calling formatName on the student name passed in. You don't appear to be using the formatted name as the key when calling get on the map, meaning the keys you pass to get are not the same as those you passed to put.

Upvotes: 4

Related Questions