hello Friends:;

I have question and I wont your help.. I do it,but I'm stuk in printing :icon_cry:.. This the code,
I hope you can help ,, Thanks

Write a class TelephoneDirectory. It has a telephoneList attribute as a HashMap. The TelephoneDirectory has the following methods:
a.addTelephoneEntry ( TelephoneNumber number, Person person )
b.removeTelephoneEntry ( TelephoneNumber number )
c.printTelephoneDirectory()

CODE:icon_wink: words in red I'm not sure this or other statment correct

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class TelephoneDirectory {
HashMap telephoneList;
public TelephoneDirectory (){
telephoneList = new HashMap ();
}

public void addTelephoneEntry(TelephoneNumber Number , Person person ){

// Add the person with its key to the telephoneList
/*
Set myKeys = telephoneList.keySet();
Iterator i= myKeys.iterator();
while(i.hasNext())
{
Number = (TelephoneNumber)i.next();
person = (Person) telephoneList.get(Number);
*/ telephoneList.put(Number,person);

}

public void removeTelephoneEntry(TelephoneNumber Number){

telephoneList.remove(Number);
}
public void PrintTdirectory(){
Set myKeys = telephoneList.keySet();
Iterator i= myKeys.iterator();
while(i.hasNext()){
TelephoneNumber Number = i.next();
System.out.print( Number+ " ");

Stuff in red seems unnecessary. In the print you can use the number to get the corresponding name and print that out as well.
ps: Make sure your code is properly indented, and use CODE tags when posting.

What do you main when you said that I can use the number to get the corresponding name and print that out as well

You have a HashMap, with the number as key and the name as value. There's a method "get" for HashMaps that gives you the value corresponding to a given key, so you can then get the name, and print both the name and number together.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.