Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Maps Java Maps Java Maps Quiz

Rob Siepkowski
Rob Siepkowski
7,641 Points

Java "get" method on a Map. Question from Quiz

/*** This is a quiz question. It is asking us to fill in blank below to retrieve Ravi's grade. I have tried a dozen different variations of studentGradeMap.get() with random spellings and variations of ravi and student and quotes and parens and everything I can think of and nothing is working. please help!! Thank you! ***/

Fill in the blank to retrieve Ravi's Grade from the Map:

import java.util.HashMap; import java.util.Map;

public class Main {

public static void main(String[] args) {
    Student ravi = new Student();
    Student sid = new Student();
    Student grace = new Student();
    Grade A = new Grade();
    Grade B = new Grade();
    Grade C = new Grade();
    Grade D = new Grade();

    Map<Student, Grade> studentGradeMap = new HashMap<>();
    studentGradeMap.put(ravi, B);
    studentGradeMap.put(sid, C);
    studentGradeMap.put(grace, B);

    Grade ravisGrade = // ????? fill in blank here ??????
}

}

class Student { // omitted }

class Grade { // omitted }

4 Answers

Have you tried

studentGradeMap.get(ravi)

?

Rob Siepkowski
Rob Siepkowski
7,641 Points

Thanks Kris! Much appreciated!!

I thought I had already tried that.

I had tried the exact same thing you wrote, but I was putting ravi in quotes ... "ravi". That's where i was going wrong.

Your answer works perfectly!

Thanks so much!

Hi there.I was facing the same issue but you already asked the same question thank you so much.

studentGradeMap.get(ravi)

Map <Student,Grade> studentGradeMap = new HashMap<>();