【问题标题】:Getting and Printing User Inputs in HashMaps in Java在 Java 的 HashMap 中获取和打印用户输入
【发布时间】:2020-10-26 23:23:11
【问题描述】:

一个月前我开始接触Java,那时一直在学习。我遇到了一个问题。我需要将 N 个用户输入对(键:值)获取到 Java 中的 HashMap,如下面的代码中所述。输入一个键:值对后,这给了我一个 InputMismatchException。据我所知,我无法弄清楚声明的循环中是否存在语法错误并将用户输入值对分配给声明的 HashMap。如果有人可以详细说明这一点,我将非常感激,希望用简单的术语,因为我是一个非常初学者。非常感谢您的关心。

public static void main (String [] arg){
    HashMap<String, Integer> phonebook = new HashMap<>();
    Scanner obj = new Scanner(System.in);
    //N= Number of contacts to be entered by the user
    int N = obj.nextInt();
    //Getting N num of user inputs for names and contacts
    while(N>0){
        for (int i=0;i<N;i++)
        {
            //we need to input name and contact value pairs
            //in same line
            String name = obj.nextLine();
            int contact = obj.nextInt();
            //assigning user input key:value pairs to hashmap
            phonebook.put(name, contact);
        }
        //setting key:value pairs to display
        Set<String> keys = phonebook.keySet();
        for(String i:keys)
        {
            System.out.println(i +"="+phonebook.get(i));
        }
        N--;
    }
}

【问题讨论】:

  • 我忘了说,用户输入的键:值对应该像“John” 1234(之间的空格)一样输入一行。当我尝试以这种格式测试用户输入时,我得到 InputMismatchException。为我的错误道歉。你能帮帮我吗?
  • 顺便说一句,正确缩进代码可以促进理解。

标签: java hashmap user-input


【解决方案1】:

您总是需要在obj.nextInt(); 之后输入obj.nextLine();。这是因为obj.nextInt();只消耗数字,但是当你输入一个数字并按回车键时,输入流最后也会记录一个换行符,所以下一个obj.nextLine();拿起一个空字符串,你就是从那时起总是减一。以下是事件序列示例:

  1. 您输入输入的数量。
  2. 程序将其读入变量N
  3. 程序将剩余的空字符串读入变量name
  4. 您输入名称。
  5. 程序尝试将数字读入变量contact,但您输入的不是数字,因此失败。

为了您自己的理智,使用一些缩进。这是您更正的代码,带有缩进:

public static void main(String[] arg) {
    HashMap<String, Integer> phonebook = new HashMap<>();
    Scanner obj = new Scanner(System.in);

    //N= Number of contacts to be entered by the user
    int N = obj.nextInt();
    obj.nextLine(); //consume the newline

    //Getting N num of user inputs for names and contacts
    while (N > 0) {
        for (int i = 0; i < N; i++) {
            //we need to input name and contact value pairs
            //in same line
            String name = obj.nextLine();
            int contact = obj.nextInt();
            obj.nextLine(); //consume the newline

            //assigning user input key:value pairs to hashmap
            phonebook.put(name, contact);
        }

        //setting key:value pairs to display
        Set<String> keys = phonebook.keySet();

        for (String i : keys) {
            System.out.println(i + "=" + phonebook.get(i));
        }

        N--;
    }
}

或者,如果您确实希望在 cmets 中输入的同一行中输入姓名和联系人,您可以替换此行:

String name = obj.nextLine();

用这一行:

String name = obj.findInLine("\\D+");

这只是告诉 Java 从输入流中读取,直到它碰到一个数字字符。

【讨论】:

    【解决方案2】:

    您需要在获得 N 后添加 obj.nextLine() 语句。当您在提示中输入内容时,会在您按 Enter 后添加一个行尾字符 (\n)。 nextInt() 只读取一个数字,所以当您在nextInt() 之后立即调用nextLine() 时,它只会读取行尾字符\n,因为nextInt() 没有接听它。通过在调用nextInt() 之后添加额外的nextLine() 语句,您可以摆脱\n 并且程序可以正确读取值。

    此代码有效:

    public static void main (String [] arg){
        HashMap<String, Integer> phonebook = new HashMap<>();
        Scanner obj = new Scanner(System.in);
        //N= Number of contacts to be entered by the user
        int N = obj.nextInt();
        obj.nextLine();
        //Getting N num of user inputs for names and contacts
        while(N>0){
            for (int i=0;i<N;i++)
            {
                //we need to input name and contact value pairs
                //in same line
                String name = obj.nextLine();
                int contact = obj.nextInt();
                obj.nextLine();
                //assigning user input key:value pairs to hashmap
                phonebook.put(name, contact);
            }
            //setting key:value pairs to display
            Set<String> keys = phonebook.keySet();
            for(String i:keys)
            {
                System.out.println(i +"="+phonebook.get(i));
            }
            N--;
        }
    }
    

    控制台输入输出如下。您可能想使用i &lt; N - 1 而不是i &lt; N,因为我只想输入 2 个联系人,但必须添加 3 个。这可能会使用户感到困惑。

    2
    foo
    100
    bar
    1000
    bar=1000
    foo=100
    n
    1000000
    bar=1000
    foo=100
    n=1000000
    

    【讨论】:

    • 非常感谢您的快速回复。非常感谢...
    猜你喜欢
    • 1970-01-01
    • 2015-12-28
    • 2011-08-20
    • 1970-01-01
    • 2016-06-15
    • 2015-02-14
    • 2021-12-08
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多