title: Getting and Printing User Inputs in HashMaps in Java
body: <p>I stepped into Java one month ago, and have been studying then. I've come across with a problem. I need to get N number of user input pairs (key:value) to a HashMap in Java, as mentioned in below code. This gives me an InputMismatchException after entering one key:value pair. For the best of my knowledge, I can't figure out whether there is a syntax error in declared loops and assigning user input value pairs to declared HashMap. I will be really grateful if someone can elaborate this, hopefully in simple terms, as I'm a very beginner. Thank you so much for your concern.</p>
  <pre><code>public static void main (String [] arg){
      HashMap&lt;String, Integer&gt; phonebook = new HashMap&lt;&gt;();
      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&gt;0){
          for (int i=0;i&lt;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&lt;String&gt; keys = phonebook.keySet();
          for(String i:keys)
          {
              System.out.println(i +&quot;=&quot;+phonebook.get(i));
          }
          N--;
      }
  }
  </code></pre>