public static void main(String[] args) throws Exception {
String[] cats = new String[]{"Tiger", "Missy", "Smokey", "Marmalade", "Oscar", "Snowball", "Boss", "Smudge", "Max", "Simba"};
HashMap<String, Cat> map = addCatsToMap(cats);
for (Map.Entry<String, Cat> pair : map.entrySet()) {
System.out.println(pair.getKey() + " - " + pair.getValue());
}
}
public static HashMap<String, Cat> addCatsToMap(String[] cats) {
//write your code here
for(String x: cats)
{
//map.put(x, new Cat(x));//
new HashMap<x, new Cat(x)>(); //
}
}
public static class Cat {
String name;
public Cat(String name) {
this.name = name;
}
@Override
public String toString() {
return name != null ? name.toUpperCase() : null;
}
}
hashmap correction at line 17
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
sivakumar
7 October 2018, 05:48
HashMap<String,Cat> cat = new HashMap<String,Cat>();
for(String catss:cats){
Cat ca = new Cat(catss);
cat.put(catss,ca);
}
return cat;
+9
Learner
27 September 2018, 05:57solution
Line 17 is completely incorrect.
1. new HashMap<String, Cat>() is the correct syntax and can only be used for declaration. You can not assign values.
2. use .put() method to assign values.
3. Where is your return statement?
+4
gery
27 September 2018, 02:36
base logic: hashmap<String, String> p = new Hashmap<String, String>()
in code: hashmap<string, Cat> p = func(cats);
func()
{
for(String x: cats)
return new hashmap<x, new cat(x)>();
}
0
Ankush Rajput
27 September 2018, 05:53useful
Firstly, Line 17 in your addCatsToMap function is wrong. You have to put Cats object with key as their name inside hashmap. new HashMap<>() is used only for declaration and not adding elements. use map.put(). whatever you put inside <> is only type identifier which identifies which type is going to be stored in hashmap or any other collection but doesn't actually store the value. To store, use put() function.
Secondly, in your above base logic code, you made same mistake inside for loop. You to add in one hashmap and not create a new hashmap for every object.
Also, you cannot use return like that inside a loop. You have to understand what exactly return keyword does - it breaks out of the function and returns value, your loop will stop then and there as return is called. Store the value in Hashmap using put and then return the complete hashmap.
refer to below links for return keyword -
https://stackoverflow.com/questions/10661081/java-does-returning-a-value-break-a-loop
https://www.geeksforgeeks.org/return-keyword-java/
+1
gery
28 September 2018, 19:25
whats wrong with hashmap variable "map" at line 16;
error on: line 16 & 19
line 4: has hashmap "map" variable.
0
Ankush Rajput
28 September 2018, 20:19solution
Recall the concept about scope of variables. If not familiar, you can refer to below links or search google -
https://www.geeksforgeeks.org/variable-scope-in-java/
http://www.javawithus.com/tutorial/scope-and-lifetime-of-variables
You've declared map inside main function and trying to access it inside addCatsToMap function.
Create a new HashMap inside addCatsToMap function, put values in it and then return the new map.
addCatsToMap function will return the value of new map and store in map declared in main function.
+2
gery
28 September 2018, 21:59solution
google keywords attempeted(not each word is perfectly correct):
https://www.google.com.pk/search?ei=fJyuW7KXDs2g5wKuvqZQ&q=hashmap+decalare+and+assign+values+in-directly+functions&oq=hashmap+decalare+and+assign+values+in-directly+functions&gs_l=psy-ab.3...27471.40424.0.40735.29.25.0.0.0.0.615.3877.3-4j4j1.9.0....0...1.1.64.psy-ab..24.2.742...33i10k1.0.zRcdom-aJX8
refrence:
https://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way
+8
John Michael Montuya
12 June 2019, 23:51
public static HashMap<String, Cat> addCatsToMap(String[] cats) {
//write your code here
HashMap<String, Cat> map = new HashMap<>();
for (int i = 0; i < cats.length; i++){
map.put(cats[i], new Cat(cats[i]));
}
return map;
}
0