Java ArrayList进入HashMap并不打印正确的条目

时间:2016-11-03 01:51:48

标签: java arraylist hashmap

我有一个电话簿程序,包含一个主要和一个PhoneBook类,如下所示

    import java.util.Scanner;

    public class JavaApplication9 {

    public static void main(String[] args) {

        Scanner inp = new Scanner(System.in);
        PhoneBook p = new PhoneBook();
        String name;
        String name2;
        String number;
        int sw;

        do
        {           
            System.out.println("Choose");
            System.out.println("1. Input new contact and number info");
            System.out.println("2. Search"); 
            System.out.println("3. Show contacts and numbers");
            System.out.println("4. exit");

                sw = inp.nextInt();

                switch (sw)
                {
                    case 1: System.out.println("Input name");
                            name = inp.nextLine();
                            inp.nextLine(); //to bypass the nextLine-skip 

                            System.out.println("Input number");
                            number = inp.nextLine();

                            p.addNumber(name, number);
                            break;

                    case 2: System.out.println("Input name to search");
                            name2 = inp.nextLine();

                            p.showNumbersOf(name2);
                            break;

                    case 3: p.show();
                            break;

                    case 4: 
                }
            } while(sw!=4);
        }   

PhoneBook课程

        public class PhoneBook {

        ArrayList<String> arrList = new ArrayList<>();
        HashMap<String, ArrayList<String>> catalog = new HashMap<>();
        String name;
        String number;



            public void addNumber(String aName, String aNumber)
            {
                name = aName;

                if (catalog.get(name) == null) 
                {
                    catalog.put(name, new ArrayList());
                }

                catalog.get(name).add(aNumber);
                catalog.put(name, catalog.get(name));     
            }

            public void showNumbersOf(String aName)
            {
                name = aName;


                if (catalog.containsKey(name))
                {

                    for (String it1 : catalog.keySet())
                    {
                        if (aName.equals(it1))
                        {
                            System.out.println(catalog.get(it1));
                        }
                    }
                } 
                else 
                {
                    System.out.println("No such contact");
                }
            }


            public void show()
            {
                for (String name: catalog.keySet())
                System.out.println(name + catalog.get(name));  
            }    

        } 

所以当我添加一个类似的联系人时 缺口 5550100 并决定展示, 它应该打印尼克[55501000] 但它打印[55501000](名称应该是一个空格)

当使用showNumberOf时,它似乎跳过了整个

        name2 = inp.nextLine;  

除非我从案例1中注释掉以前的inp.nextLine命令,即使那时它与show()有完全相同的问题。

在将条目放入HashMap时和/或恢复时,我无法弄清楚我做错了什么,所以请帮助我。

0 个答案:

没有答案