我已经创建了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


请帮帮我。

最佳答案

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

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

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


对此:

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


或类似的东西。

关于java - 如何从Java中的Dictionary类检索数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33272617/

10-09 06:20