Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Java
»
Beginning Java
Maps in java refuse to iterate
adebari olalekan
,
Ranch Hand
Oct 30, 2016 13:46:47
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
please can anyone tell me what is wrong in my code; it refuse to run.:
public Testmeth(String main, int nul){ this.main = main; this.nul = nul; } public String toString() { return("my name is :" + main + " " +"id :" + nul); } @SuppressWarnings("rawtypes") public Map< Integer,String> showinfo(){ Map<Integer, String> players = new HashMap<Integer, String>(); players.put (14, "henry"); players.put (7 ,"raul"); players.put (9, "ronldo"); players.put (10, "ronaldinho"); players.put (11, "totti"); System.out.println(); for(Map.Entry<Integer, String>entry: Map.entrySet()){ int key = entry.getKey(); String value = entry.getValue(); System.out.println(key + ":"+ value); return players; } } }
and this is the main:
public class Testrun { public static void main(String[] args) { Testmeth ow = new Testmeth("sue", 27); System.out.println(ow); System.out.println(ow.showinfo());
and it return this exception:
Exception in thread "main" my name is :sue id :27 java.lang.Error: Unresolved compilation problems: Cannot make a static reference to the non-static method entrySet() from the type Map Type mismatch: cannot convert from element type Object to Map.Entry<Integer,String> at Testmeth.showinfo(Testmeth.java:29) at Testrun.main(Testrun.java:6)
thanks.
Carey Brown
,
Saloon Keeper
staff
Oct 30, 2016 16:37:31
(0 cow)
(0 skulls)
1
+Pie
Number of slices to send:
Send
Looks like you meant to write
for(Map.Entry<Integer, String>entry: players.entrySet())
Carey Brown
,
Saloon Keeper
staff
Oct 30, 2016 18:58:12
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Duplicate post. Please go to
https://coderanch.com/t/672176/java/Maps-java
Post by:
autobot
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
This thread has been viewed 346 times.
Similar Threads
How can I add elements in Hashmap?
how come generis type information at runtime/reflection ?
Generics collections and reflection
Linked list, java generics and "method in undefined for type" sort-of-problems
How can I Iterate a map
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:06:21
.