title: How to avoid java.util.NoSuchElementException
body: <p>The code below is giving me the error java.util.NoSuchElementException right after I Ctrl+Z
  to indicate that the user input is complete. By the looks of it seems as if it does not know how to just end one method without messing with the other scanner object.
  I try the hasNext method and I ended up with an infinite loop, either way is not working. As a requirement for this assignment I need to be able to tell the user to use Ctrl+Z or D depending on the operating system. Also I need to be able to read from a text file and save the final tree to a text file please help.</p>
  
  <pre><code>    /* sample input:
         CSCI3320
         project
         personal
         1 HW1
         1 HW2
         1 2 MSS.java
         2 p1.java
  
          */
  
      import java.util.Scanner;
      import java.util.StringTokenizer;
  
      public class Directory {
  
          private static TreeNode root = new TreeNode("/", null, null);
  
          public static void main(String[] args) {
              userMenu();
              System.out.println("The directory is displayed as follows:");
              root.listAll(0);
  
          }
  
          private static void userMenu(){         //Displays users menu
              Scanner userInput = new Scanner(System.in);//Scanner option 
              int option = 0;
              do{ //I believe the problem is here since I am not using userInput.Next()
                  System.out.println("\n 1. add files from user inputs ");
                  System.out.println("\n 2. display the whole directory ");
                  System.out.println("\n 3. display the size of directory ");
                  System.out.println("\n 0. exit");
                  System.out.println("\n Please give a selection [0-3]: ");
                  option = userInput.nextInt();
                      switch(option){
                          case 1: addFileFromUser();
                                  break;
                          case 2: System.out.println("The directory is displayed as follows:");
                                  root.listAll(0);
                                  break;
                          case 3: System.out.printf("The size of the directory is %d.\n", root.size());
                                  break;
                          default:
                                  break;
                          }
              }while( option !=0);
              userInput.close();
  
          }
          private static void addFileFromUser() {
  
              System.out.println("To terminate inp1ut, type the correct end-of-file indicator ");
              System.out.println("when you are prompted to enter input.");
              System.out.println("On UNIX/Linux/Mac OS X type &lt;ctrl&gt; d");
              System.out.println("On Windows type &lt;ctrl&gt; z");
              Scanner input = new Scanner(System.in);
  
                  while (input.hasNext()) { //hasNext being used Crtl Z is required to break
                      addFileIntoDirectory(input); // out of the loop.
                  }
                  input.close();
          }
  
  
          private static void addFileIntoDirectory(Scanner input) {
  
              String line = input.nextLine();
              if (line.trim().equals("")) return;
  
              StringTokenizer tokens = new StringTokenizer(line);
  
              int n = tokens.countTokens() - 1;
  
              TreeNode p = root;
              while (n &gt; 0 &amp;&amp; p.isDirectory()) {
  
                  int a = Integer.valueOf( tokens.nextToken() );
                  p = p.getFirstChild();
  
                  while (a &gt; 1 &amp;&amp; p != null) {
                      p = p.getNextSibling();
                      a--;
                  }
                  n--;
              }
  
              String name = tokens.nextToken();
  
              TreeNode newNode = new TreeNode(name, null, null);
              if (p.getFirstChild() == null) {
                  p.setFirstChild(newNode);
              }
              else {
                  p = p.getFirstChild();
                  while (p.getNextSibling() != null) {
                      p = p.getNextSibling();
                  }
                  p.setNextSibling(newNode);
              }
  
          }
  
  
          private static class TreeNode {
  
              private String element;
              private TreeNode firstChild;
              private TreeNode nextSibling;
  
              public TreeNode(String e, TreeNode f, TreeNode s) {
                  setElement(e);
                  setFirstChild(f);
                  setNextSibling(s);
              }
  
  
              public void listAll(int i) {
  
                  for (int k = 0; k &lt; i; k++) {
                      System.out.print('\t');
                  }
  
                  System.out.println(getElement());
  
                  if (isDirectory()) {
                      TreeNode t = getFirstChild();
  
                      while (t != null) {
  
                          t.listAll(i+1);
                          t = t.getNextSibling();
                      }
  
                  }
              }
  
  
              public int size() {
  
                  int s = 1;
  
                  if (isDirectory()) {
                      TreeNode t = getFirstChild();
  
                      while (t != null) {
                          s += t.size();
                          t = t.getNextSibling();
                      }
                  }
  
                  return s;
              }
  
  
              public void setElement(String e) {
                  element = e;
              }
  
  
              public String getElement() {
                  return element;
              }
  
  
              public boolean isDirectory() {
                  return getFirstChild() != null;
              }
  
              public void setFirstChild(TreeNode f) {
                  firstChild = f;
              }       
  
              public TreeNode getFirstChild() {
                  return firstChild;
              }
  
              public void setNextSibling(TreeNode s) {
                  nextSibling = s;
              }       
  
              public TreeNode getNextSibling() {
                  return nextSibling;
              }
  
  
          }
  
  
  
      }
  </code></pre>
  
  <p>Exception Details:</p>
  
  <pre><code>    /*Exception in thread "main" java.util.NoSuchElementException
              at java.util.Scanner.throwFor(Scanner.java:907)
              at java.util.Scanner.next(Scanner.java:1530)
              at java.util.Scanner.nextInt(Scanner.java:2160)
              at java.util.Scanner.nextInt(Scanner.java:2119)
              at Directory.userMenu(Directory.java:36)
              at Directory.main(Directory.java:21)*/
  </code></pre>