Error about Source Level only if 5.0? I'm using Java 1.6? Maps

Hello everyone. I was just trying to run some simple Source Code from my book here:

package mapa;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

publicclass MapTest

{

Map<String, String> phonebook =new HashMap<String, String>();

// constructor

public example2(String n[], String nums[]){

for(int i=0; i< n.length; i++)

phonebook.put( n[i], nums[i] );

}

publicstaticvoid main(String[] args){

// data

String [] names ={"Lefty","Guarav","Wong","Rupamay"};

String [] extns ={"4873","4810","3769","0"};

// get an instance of this class

example2 ex =new example2( names, extns );

// dump out the map

System.out.println("map: " + ex.phonebook);

// get the mappings

Set<Map.Entry><String,String>> s = ex.phonebook.entrySet();

// iterate over the mappings

//for (Iterator i = s.iterator(); i.hasNext(); ) {

for (Map.Entry me : s){

Object ok = me.getKey();

Object ov = me.getValue();

System.out.print("key=" + ok );

System.out.println(", value=" + ov );

}

}

}

and i'm getting the following errors when I run it in Eclipse:

Exception in thread"main" java.lang.Error: Unresolved compilation problems:

example2 cannot be resolved to a type

example2 cannot be resolved to a type

Syntax error, parameterized types are only availableif source level is 5.0

The type Map.Entry is not generic; it cannot be parameterized with arguments <String, String>

Syntax error,'for each' statements are only availableif source level is 5.0

at mapa.MapTest.main(MapTest.java:24)

Line 24 is:

example2 ex =new example2( names, extns );

I've compiled things with Eclipse before such as Networking programs and they worked fine, not sure whats wrong with this example.

Thanks!

[3453 byte] By [lokiea] at [2007-11-15]
# 1

Oops it looks like on that line they are using the Class itself, and they had it renamed to Example2 not MapTest. I changed the code to the following but still getting errros:

package mapa;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class MapTest

{

Map<String, String> phonebook = new HashMap<String, String>();

// constructor

public example2(String n[], String nums[]) {

for(int i=0; i< n.length; i++)

phonebook.put( n[i], nums[i] );

}

public static void main(String[] args) {

// data

String [] names = { "Lefty", "Guarav", "Wong", "Rupamay" };

String [] extns = { "4873", "4810", "3769", "0" };

// get an instance of this class

MapTest ex = new MapTest( names, extns );

// dump out the map

System.out.println("map: " + ex.phonebook);

// get the mappings

Set<Map.Entry><String,String>> s = ex.phonebook.entrySet();

// iterate over the mappings

//for (Iterator i = s.iterator(); i.hasNext(); ) {

for (Map.Entry me : s) {

Object ok = me.getKey();

Object ov = me.getValue();

System.out.print("key=" + ok );

System.out.println(", value=" + ov );

}

}

}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

The constructor MapTest(String[], String[]) is undefined

ex.phonebook cannot be resolved or is not a field

Syntax error, parameterized types are only available if source level is 5.0

The type Map.Entry is not generic; it cannot be parameterized with arguments <String, String>

ex.phonebook cannot be resolved or is not a field

Syntax error, 'for each' statements are only available if source level is 5.0

at mapa.MapTest.main(MapTest.java:24)

lokiea at 2007-7-12 > top of java,Core,Core APIs...
# 2
You have to properly set the source level of your project in eclipse to be at least 5.0 (have a look at the project properties).
stefan.schulza at 2007-7-12 > top of java,Core,Core APIs...
# 3
Hey thanks again!I didn't realize it wasn't set! :D
lokiea at 2007-7-12 > top of java,Core,Core APIs...
# 4
Hi,Anyway, your "constructor" should be :public MapTest(String n[], String nums[])not // constructorpublic example2(String n[], String nums[]) :)
ibanna at 2007-7-12 > top of java,Core,Core APIs...