• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String as key to look up an Integer

 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>;



 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding () after the last >. After all, each method and constructor call needs them.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, didn't notice they were missing.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics, with their added < and >, do make it a bit harder to see missing ().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic