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
8 Réponses
+ 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);
}
}
+ 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.
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
0
Ajanant,
Oooooh yea, you re absolute right, thanks you
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);
}
}
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);
}
}
- 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 :)
- 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.