CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Raisa Toscano
Level 22
Tepic
  • 27.12.2018
  • 1467views
  • 2comments

why it prints null?

Question about the task Display a list of keys
Java Syntax,  Level 8,  Lesson 3
Resolved

There is a HashMap<String, String>. 10 different strings have been added to it.
Display a list of keys on the screen, each element on a new line.

Requirements:
  • The program must create a HashMap variable with (String, String) elements. The variable must be initialized immediately.
  • The program should not read values from the keyboard.
  • The program should add 10 different strings to the collection in accordance with the conditions.
  • The printKeys() method should display the keys in the collection, each element on a new line.
package com.codegym.task.task08.task0804; import java.util.HashMap; import java.util.Map; /* Display a list of keys */ public class Solution { private static String pair; public static void main(String[] args) throws Exception { HashMap<String, String> map = new HashMap<String, String>(); map.put("Sim", "Sim"); map.put("Tom", "Tom"); map.put("Arbus", "Arbus"); map.put("Baby", "Baby"); map.put("Cat", "Cat"); map.put("Dog", "Dog"); map.put("Eat", "Eat"); map.put("Food", "Food"); map.put("Gevey", "Gevey"); map.put("Hugs", "Hugs"); printKeys(map); } public static void printKeys(Map<String, String> map) { //write your code here HashMap<String, String> m = new HashMap<>(); for(HashMap.Entry<String,String> pair : map.entrySet()); System.out.println(pair); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
27 December 2018, 21:30solution
Please attach your code like this next time: your problem is that you have a ';' at the end of your for loop:
for(HashMap.Entry<String,String> pair : map.entrySet()); //< get rid of that
also, you are suppose to only print the keys in this lesson. Change the code here to print the key only:
System.out.println(pair);
+7
Raisa Toscano
Level 22 , Tepic, Mexico
27 December 2018, 21:38
thanks you're right!!
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.