| Author |
String as key to look up an Integer
|
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
I like to use String as key to look up an Integer.
Therefore I try the following code, but it has compile error:
Syntax error on token(s), misplaced construct(s)
The following code has compile error too:
The local variable map may not have been initialized Map.java line 10 Java Problem
The local variable map may not have been initialized Map.java line 11 Java Problem
Please help.
Thanks.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
Hi Albert,
"Map" isn't a class, it's an interface. You need to create an instance of a class that implements Map, for example HashMap:
Map<String, Integer> map = new HashMap<String, Integer>;
|
[Jess in Action][AskingGoodQuestions]
|
 |
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
Ernest Friedman-Hill wrote:Hi Albert,
"Map" isn't a class, it's an interface. You need to create an instance of a class that implements Map, for example HashMap:
Map<String, Integer> map = new HashMap<String, Integer>;
This still has errors:
Syntax error on token(s), misplaced construct(s)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Try adding () after the last >. After all, each method and constructor call needs them.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
|
Sorry, didn't notice they were missing.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Generics, with their added < and >, do make it a bit harder to see missing ().
|
 |
 |
|
|
subject: String as key to look up an Integer
|
|
|