CodeGym
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
Sarvesh
Level 11
Bangalore
  • 05.03.2019
  • 1367views
  • 2comments

Can anyone help me idnetify my error ?

Question about the task Census
Java Syntax,  Level 8,  Lesson 8
Under discussion


Create a Map<String, String> and add ten entries that represent (last name, first name) pairs.
Check how many people have the same first name or last name.

Requirements:
  • The program should not display text on the screen.
  • The program should not read values from the keyboard.
  • The createMap() method must create and return a HashMap that has (String, String) elements and contains 10 entries that represent (last name, first name) pairs.
  • The getSameFirstNameCount() method must return the number of people whose first name is the same as the name parameter.
  • The getSameLastNameCount() method must return the number of people whose last name is the same as the lastName parameter.
package com.codegym.task.task08.task0815; import java.util.HashMap; import java.util.HashSet; import java.util.*; /* Census */ public class Solution { public static HashMap<String, String> createMap() { //write your code here HashMap<String,String> map = new HashMap<String,String>(); map.put("sarvesh","kumar"); map.put("sarves","kuma"); map.put("sarve","kumarr"); map.put("sarv","kumarrr"); map.put("sar","rajr"); map.put("sarx","kumarrrr"); map.put("sarxx","rajrr"); map.put("sarxxx","ghor"); map.put("sarxxxx","ghorf"); map.put("sarxxxxx","baba"); return map; } public static int getSameFirstNameCount(HashMap<String, String> map, String name) { //write your code here int sameFirstNameCount = 0; HashSet<String> set = new HashSet<>(map.values()); Iterator<String> itr = set.iterator(); while (itr.hasNext()) { if (itr.next().equals(name)) sameFirstNameCount++; } return sameFirstNameCount; } public static int getSameLastNameCount(HashMap<String, String> map, String lastName) { //write your code here int sameLastNameCount = 0; HashSet<String> set = new HashSet<>(map.keySet()); Iterator<String> itr = set.iterator(); while (itr.hasNext()) { if (itr.next().equals(lastName)) sameLastNameCount++; } return sameLastNameCount; } public static void main(String[] args) { } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Sela
Level 20 , Poland
30 June 2020, 05:56
Minh is right. your map created on line 15 is loaded with unique values (first names) which is special case. but the test code which checks the method correctness loads different map and sees that the output is not correct because you method can only give 0 (zero) or 1 (one) as a result.
0
Minh Phạm Đức
Level 22 , Cedar Falls, United States
5 March 2019, 11:41
The problem is HashSet<String> set = new HashSet<>(map.values()); you are storing the map values in a Set which does not allow duplicate values (since elements of a set are unique), hence in your case, instead of having a set of 10 names, it only has a set of 9 names (since one name is duplicated and hence, was removed), thus, resulting in incorrect return. the reason your get same last name works is because all 10 elements were unique. Suggestion: Change this Iterator<String> itr = set.iterator(); to Iterator<String> itr = map.values().iterator(); there's no need to create a set just for the iterator.... and of course, remove the Set creation.
+3
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.