title: NoSuchElementException: No line found -- Scanner Java
body: <p>I'm trying to write a method that reads in a file given the path and name of the file. I have called this method once before in my program, and it ran fine. However, on my second call, it's giving me an error that reads
  <code>NoSuchElementException: No line found</code>. I looked through many other threads and found that the issue was that closing the Scanner also closes System.in. However, I'm not closing any scanners, or any input/output streams. The method looks like this:</p>
  
  <pre><code>public static Map readFileMap(String path, String fileName)
      throws IOException
    {
      path = path.replace("/", "\\");
      String key = null;
      String val = null;
      Map&lt;String, String&gt; map = new HashMap&lt;String,String&gt;();
      input = new Scanner(new File(path + fileName));
      int numLines = Helper.numLines(path + fileName);
      System.out.println(numLines);
      for (int loop = 0; loop &lt; numLines; loop++)
      //while(input.hasNextLine())
      {
        String all = input.nextLine();
        System.out.println(all);
        String[] pieces = all.split("\\s", 2);
        key = pieces[0];
        val = pieces[1];
        map.put(key, val);
      }
      return map;
    }
  </code></pre>
  
  <p>The variable numLines contains the (correct) value of 143. In case it has something to do with the file's data, the data looks like this:</p>
  
  <pre><code>! %21
  " %22
  # %23
  $ %24
  % %25
  &amp; %26
  ' %27
  ( %28
  ) %29
  * %2A
  + %2B
  , %2C
  - %2D
  . %2E
  / %2F
  [ %5B
  \ %5C
  ] %5D
  ^ %5E
  _ %5F
  ` %60
  ~ %7E
    %7F
  ` %80
  ‚ %82
  ƒ %83
  „ %84
  … %85
  † %86
  ‡ %87
  ˆ %88
  ‰ %89
  Š %8A
  ‹ %8B
  Œ %8C
  Ž %8E
  ‘ %91
  ’ %92
  “ %93
  ” %94
  • %95
  – %96
  — %97
  ˜ %98
  ™ %99
  š %9A
  › %9B
  œ %9C
  ž %9E
  Ÿ %9F
  ¡ %A1
  ¢ %A2
  £ %A3
  ¤ %A4
  ¥ %A5
  ¦ %A6
  § %A7
  ¨ %A8
  © %A9
  ª %AA
  « %AB
  ¬ %AC
  ® %AE
  ¯ %AF
  ° %B0
  ± %B1
  ² %B2
  ³ %B3
  ´ %B4
  µ %B5
  ¶ %B6
  · %B7
  ¸ %B8
  ¹ %B9
  º %BA
  » %BB
  ¼ %BC
  ½ %BD
  ¾ %BE
  ¿ %BF
  À %C0
  Á %C1
  Â %C2
  Ã %C3
  Ä %C4
  Å %C5
  Æ %C6
  Ç %C7
  È %C8
  É %C9
  Ê %CA
  Ë %CB
  Ì %CC
  Í %CD
  Î %CE
  Ï %CF
  Ð %D0
  Ñ %D1
  Ò %D2
  Ó %D3
  Ô %D4
  Õ %D5
  Ö %D6
  × %D7
  Ø %D8
  Ù %D9
  Ú %DA
  Û %DB
  Ü %DC
  Ý %DD
  Þ %DE
  ß %DF
  à %E0
  á %E1
  â %E2
  ã %E3
  ä %E4
  å %E5
  æ %E6
  ç %E7
  è %E8
  é %E9
  ê %EA
  ë %EB
  ì %EC
  í %ED
  î %EE
  ï %EF
  ð %F0
  ñ %F1
  ò %F2
  ó %F3
  ô %F4
  õ %F5
  ö %F6
  ÷ %F7
  ø %F8
  ù %F9
  ú %FA
  û %FB
  ü %FC
  ý %FD
  þ %FE
  ÿ %FF
  </code></pre>
  
  <p>As I mentioned before, when I called the method the first time around, everything ran correctly. So my guess is that it has to do with me re-initializing the variable <code>input</code>. Any help would be greatly appreciated!</p>