Meaningless Drivel is fun!*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 245
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: 24183
    
  34

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: 245
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: 19656
    
  18

Try adding () after the last >. After all, each method and constructor call needs them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Sorry, didn't notice they were missing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Generics, with their added < and >, do make it a bit harder to see missing ().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String as key to look up an Integer
 
Similar Threads
Compiling Jsp in Solaris
Sun Cirtification
Solaris & Jsp Compilation
1:1 Map class?
Garbage Collection!