title: Java: How to check with a scanner to see if a file contains integers or words?
body: <p>I need to store a text file into a linked list. I am given a text file like:
  1
  2
  3
  4
  5
  When I try to say scanner.next it says there is a no element exception.
  But I also need to account for a file like this:
  Michael 
  Hannah
  Josephus
  Ruth 
  Matthew
  So I can't just say nextInt because it could be a string.
  How can I check if its integers or strings?
  Thanks!
  My code:</p>
  
  <pre><code>Scanner sc = new Scanner(f);
  int k = 1;
  String line = new String(sc.next());
  ListNode nodes = new ListNode(n, null);
  while(k &lt; n - 1)
  {
     line = sc.nextLine();
     nodes.setNext(new ListNode(n, null));
  }
  line = sc.nextLine();
  ListNode last = new ListNode(n, null);
  nodes.setNext(last);
  return nodes;
  </code></pre>