title: How to solve array index out of bounds exception for hashmap string split in java?
body: <p>I am trying to put a text file into a hashmap with a while loop, and use string split to get the key value pairs. However, I keep getting Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1.</p>
  <pre><code>package com.company;
  
  import java.io.File;
  import java.util.HashMap;
  import java.util.Scanner;
  
  public class Extract {
      public static void main(String[] args) throws Exception {
          Extract e = new Extract();
          HashMap&lt;String, String&gt; x = e.hashFile(&quot;locations.txt&quot;);
          x.get(&quot;name&quot;);
  
      }
  
      public HashMap&lt;String, String&gt; hashFile(String filename) throws Exception {
          HashMap&lt;String, String&gt; data = new HashMap&lt;String, String&gt;();
  
          Scanner in = new Scanner(new File(filename));
          String line;
          while (in.hasNext()) {
              line = in.nextLine();
  
  
              String[] keyvalue = line.split(&quot;:&quot;);
              keyvalue[1] = keyvalue[1].trim();
  
  
              data.put(keyvalue[0], keyvalue[1]); // this is where the exception is thrown from
  
          }
  
  
          in.close();
          return data;
  
  
      }
  
  
  }
  </code></pre>
  <p>My text file locations.txt looks like :</p>
  <pre><code>name: Starting Point Park
  exits: 1
  exitDirection: E
  description: You were just given your first cell phone as a birthday-present and you are at the park taking selfies. You choose your best-looking photo and upload it to your brand-new Instagram account. Suddenly, a white rabbit runs up and steals your phone and heads down a hole just big enough for you to fit in to. Instantly you panic and worry about the lost phone, not because it was a gift, but because you need to see how many likes your photo got.
  
  </code></pre>