如何从java中的Dictionary类中检索数据

时间:2015-10-22 02:52:30

标签: java dictionary

我创建了Dictionary类的对象,我在其中插入值。并且在显示时我得到了我在字典中推送的所有值。但是在搜索时我无法从字典中获得特定的键值对。

class Demo{
        static String newLine = System.getProperty("line.separator");
        public static void main(String[] args) {
            int ch=0,num=0;
            String dictionary,contactName,contactNumber,relation;
            Scanner in=new Scanner(System.in);
            System.out.println(newLine + "Dictionary in Java" + newLine);
                System.out.println("-----------------------" + newLine);
                System.out.println("Adding items to the Dictionary" + 
                newLine);
                Dictionary dict = new Hashtable();
                while(ch!=4){
                System.out.println("1. Insert Contact Details\n2. Display  
                Contact Details\n3. Search Contact Details\n4. Exit");

                System.out.println("Enter Your Choice:");
                ch=Integer.parseInt(in.nextLine());

                switch (ch) 
                {
                case 1:
                        System.out.println("How many Contacts you want to 
                        insert:");
                        num=Integer.parseInt(in.nextLine());
                        for(int i=0;i<num;i++)
                            {
                            System.out.println("Enter the name of Contact Number 
                            "+(i+1)+":");
                            contactName=in.nextLine();


                            System.out.println("Enter the Contact Number of 
                            "+contactName+":");
                            contactNumber=in.nextLine();
                                dict.put(contactName, contactNumber);
                            }

                        System.out.println(newLine + "Items in the 
                        dictionary..." + dict + newLine);
                        System.out.println("-----------------------" + newLine);
                         break;
                case 2:
                        System.out.println(newLine + "Items in the 
                        dictionary..." + dict + newLine);
                        System.out.println("-----------------------" + newLine);
                        break;
                case 3:
                    System.out.println("Enter Name you want to search:");
                    String name=in.nextLine();
                    System.out.println(dict.get(name));
                    break;
                }
}

这是我的输出:

Dictionary in Java

-----------------------

Adding items to the Dictionary

1. Insert Contact Details
2. Display Contact Details
3. Search Contact Details
4. Exit
Enter Your Choice:
1
How many Contacts you want to insert:
2
Enter the name of Contact Number 1:
qwe
Enter the Contact Number of qwe:
123
Enter the name of Contact Number 2:
asd
Enter the Contact Number of asd:
12332

Items in the dictionary...{qwe=123, asd=12332}

-----------------------

1. Insert Contact Details
2. Display Contact Details
3. Search Contact Details
4. Exit
Enter Your Choice:
2

Items in the dictionary...{qwe=123, asd=12332}

-----------------------

1. Insert Contact Details
2. Display Contact Details
3. Search Contact Details
4. Exit
Enter Your Choice:
3
Enter Name you want to search:
1
null

请帮帮我。

1 个答案:

答案 0 :(得分:2)

您的代码运行正常,但您输入的搜索字词错误。您应该输入1que联系人的姓名,而不是输入asd

如果您要同时打印姓名和联系电话,请更改您的println,因为它只会打印您告诉它的内容。改变这个:

System.out.println(dict.get(name));

到此:

System.out.println("Name: " + name + ", contact: " + dict.get(name));

或类似的东西。