views:

63

answers:

1

Hi, I am trying to compile this program. It works perfectly for 2 Strings(Name, phone number) But not for 3 Strings (Name, phone number and sex).


CODE (Not working code - 3 Strings (Name, phone number and sex))


import java.util.Map;
import java.util.TreeMap;

public class Ann {

String name, phone;

public Ann() {
}

public static void testMap() {
    Map<String, String, String> theMap = new TreeMap<String, String,String>();
    // new HashMap<K,V>(); could also be used
    theMap.put("Roger M", "090-997-2918", "Male");
    theMap.put("Jane M", "090-997-1987", "FeMale");
    theMap.put("Stacy K", "090-997-9188", "FeMale");
    theMap.put("Gary G", "201-119-8765", "Male");
    theMap.put("Jane M", "090-233-0000", "FeMale");
    System.out.println("Testing TreeMap and Map");
    System.out.print("Stacy K has phone ");
    System.out.print(theMap.get("Stacy K"));
    System.out.print("\n");

    System.out.print("Jane M has phone ");
    System.out.print(theMap.get("Jane M"));
} // testMap()

public static void main(String[] args) {
    testMap();

}
}

ERROR

wrong number of type arguments; required 2

wrong number of type arguments; required 2


WORKING CODE (For 2 Strings (Name, phonenumber))


import java.util.Map;
import java.util.TreeMap;

public class Ann {

String name, phone;

public Ann() {
}

public static void testMap() {
    Map<String, String> theMap = new TreeMap<String, String>();
    // new HashMap<K,V>(); could also be used
    theMap.put("Roger M", "090-997-2918");
    theMap.put("Jane M", "090-997-1987");
    theMap.put("Stacy K", "090-997-9188");
    theMap.put("Gary G", "201-119-8765");
    theMap.put("Jane M", "090-233-0000");
    System.out.println("Testing TreeMap and Map");
    System.out.print("Stacy K has phone ");
    System.out.print(theMap.get("Stacy K"));
    System.out.print("\n");

    System.out.print("Jane M has phone ");
    System.out.print(theMap.get("Jane M"));
    } // testMap()

public static void main(String[] args) {
    testMap();

}
}

I want the code to work for about 5 attributes like name , phone, sex,age,address. If someone can help me compile the code at the top of the question, I can figure out the rest.

Thanks

+6  A: 

You can't just add type parameters arbitrarily to generic types - they are defined with a certain number, and have to use that many (disregarding raw types). The type parameters have specific meanings for the implementation - how would the HashMap class know what you wanted to get out if you called map.get(name)?

You should encapsulate all the properties into a class (e.g. Person or Contact) and then create a Map<String, Person> to map from the name to the person. For example:

public enum Gender
{
    FEMALE, MALE;
}

public final class Person
{
    private final String name;
    private final Gender gender;
    private final Date dateOfBirth;
    private final String address;
    private final String telephone;

    public Person(String name, Gender gender, Date dateOfBirth,
                  String address, String telephone)
    {
        // You probably want to put some validation in here
        this.name = name;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;
        this.address = address;
        this.telephone = telephone;
    }

    public String getName()
    {
        return name;
    }

    // etc for the other properties
}

...

Map<String, Person> map = new HashMap<String, Person>();
Person jon = new Person("Jon", Gender.MALE, /* etc */);
map.put("Jon", jon);
Jon Skeet