aspose file tools
The moose likes Beginning Java and the fly likes String as key to look up an Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "String as key to look up an Integer" Watch "String as key to look up an Integer" New topic
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
    
  13

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
    
  13

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 ().
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: String as key to look up an Integer
 
Similar Threads
1:1 Map class?
Garbage Collection!
Compiling Jsp in Solaris
Sun Cirtification
Solaris & Jsp Compilation