HashMap | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

HashMap

I can't resolve this problem, anyone can help please? The program you are given defines and outputs HashMap, where the names of employees are stored as keys, and their ages as values. It also takes N number from user as age limit. Write code to delete all the employees whom age is less than N number. The line of code for the output of HashMap object is already provided. Sample Input 25 Sample Output {Robert=32, John=28} https://code.sololearn.com/c58a23a2A1a5

23rd May 2021, 7:44 PM
JRAMAHES
JRAMAHES - avatar
8 Answers
+ 12
import java.util.HashMap; import java.util.Scanner; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); HashMap<String, Integer> ages = new HashMap<String, Integer>(); ages.put("David", 22); ages.put("Tom", 23); ages.put("Robert", 32); ages.put("Alice", 21); ages.put("Sophie", 19); ages.put("Maria", 24); ages.put("John", 28); String[] nameArr = new String[ages.size()]; nameArr = ages.keySet().toArray(nameArr); int ageLimit = scanner.nextInt(); for (String emp : nameArr){ if ( ages.get(emp) < ageLimit){ ages.remove(emp); } } System.out.println(ages); } }
5th Sep 2021, 7:52 AM
Fazal Haroon
Fazal Haroon - avatar
+ 1
Soumik [Busy] A little mistake, there should be less than agelimit. We have to remove all the employees whom age is less than given age limit.
24th May 2021, 2:15 AM
A͢J
A͢J - avatar
0
JuanRamoneMH Problem is that you are comparing with item value which is wrong. You have to first compare value with map value then remove item. To get map value you have to use get method. If map value is less than input value then remove item. https://code.sololearn.com/c7d3mKBkEJ3F/?ref=app
24th May 2021, 2:12 AM
A͢J
A͢J - avatar
0
Ajanant, Oooooh yea, you re absolute right, thanks you
24th May 2021, 7:28 AM
JRAMAHES
JRAMAHES - avatar
0
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); HashMap<String, Integer> ages = new HashMap(); ages.put("David", 22); ages.put("Tom", 23); ages.put("Robert", 32); ages.put("Alice", 21); ages.put("Sophie", 19); ages.put("Maria", 24); ages.put("John", 28); String[] nameArr = new String[ages.size()]; nameArr = ages.keySet().toArray(nameArr); int ageLimit = scanner.nextInt(); for (String emp : nameArr){ if ( ages.get(emp) < ageLimit){ ages.remove(emp); } } System.out.println(ages); } }
15th Nov 2022, 7:39 AM
Pooja Patel
Pooja Patel - avatar
0
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> numbers = new ArrayList<>(); while (numbers.size() < 5) { int input = scanner.nextInt(); numbers.add(input); } int max = numbers.get(0); int min = numbers.get(0); for (int number : numbers) { if (number > max) { max = number; } if (number < min) { min = number; } } System.out.println(max); System.out.println(min); } }
20th Oct 2023, 2:14 AM
Juan David Suarez Holguin
Juan David Suarez Holguin - avatar
- 1
You are comparing a string with a number, therefore the < won't work. you have to retrieve the age from the hashmap by using the get method: if(ages.get(emp) < ageLimit) Also, you do not need to create the nameArr that you have, the keyset is enough in the for loop: for (String emp : ages.keySet()) I hope this helps :)
23rd May 2021, 9:05 PM
Apollo-Roboto
Apollo-Roboto - avatar
- 1
well the code worked, would you run the code and it will run fine, but the think is that it does not print the number just the name when I run it.
23rd May 2021, 9:44 PM
JRAMAHES
JRAMAHES - avatar