• Post Reply Bookmark Topic Watch Topic
  • New Topic

Missing something

 
Phil Kirsch
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the code below from Head First Java, Edition 2, page 567.

1 import java.util.*;
2
3 public class TestMap {
4
5 public static void main(String[] args) {
6 HashMap<String, Integer> scores = new HashMap<String, Integer>();
7
8 scores.put("Kathy", 42);
9 scores.put("Bert", 343);
10 scores.put("Skyler", 420);
11
12 System.out.println(scores);
13 System.out.println(scores.get("Bert"));
14
15 }
16 }


I get the following errors at compile time:

javac map.java
map.java:6: not a statement
HashMap<String, Integer> scores = new HashMap<String, Integer>();
^
map.java:6: ';' expected
HashMap<String, Integer> scores = new HashMap<String, Integer>();
^
2 errors


I've checked my typing and gone back in the book. Can't figure out what's going on. I may feel really stupid when someone explains this, but right now I'm stumped.
 
Orem Hun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which version of JDK are you using ?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only reason that could happen is if the compiler doesn't know about the Collections framework.
That excludes every compiler from 1.2 onwards except gcj (which is a disaster, never use it!) AFAIK.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil, it seems to me that even if you were using a JDK so old it did not recognize HashMap, you should get a different error message in that case:

TestMap.java:6: cannot find symbol
symbol : class HashMap

I think there's still a typo somewhere, perhaps in line 5. I suggest you copy and paste the code you're trying to compile. Don't insert line numbers - just copy exactly what you have.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!