File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 239
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: 24166
    
  30

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: 239
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: 19538
    
  16

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: 24166
    
  30

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

Joined: Oct 27, 2005
Posts: 19538
    
  16

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
Solaris & Jsp Compilation
Compiling Jsp in Solaris
Sun Cirtification
Garbage Collection!
1:1 Map class?