title: What should I do to fix this scanner-related bug?
body: <p>Even though this code compiles:</p>
  
  <pre><code>import java.util.Scanner; // imports the Scanner class from the java.util package
  
  public class ScannerPractice {
  
  public static void main(String args[]) {
  
      Scanner word = new Scanner("word 1 2 3 4"); // creates a new Scanner objecrt with a string as its input
      String scaStr = word.nextLine(); // converts scanner to string
  
      String strArr[] = new String[10];
      // as long as the scanner has another character...
      for (int i = 0; i &lt; scaStr.length(); i++) {
  
          int j = 0;
          String k = "";
          // if the next token is an integer...
          if (word.hasNextInt()) {
  
              j = word.nextInt();
              k = String.valueOf(j);
              strArr[i] = k;
  
          }
  
          // otherwise, skip over that token
          else {
  
              word.next();
  
          }
  
      }
  
      String k = "";
      // for each character in charArr
      for (int i = 0; i &lt; strArr.length; i++) {
  
          // Accumulate each element of charArr to k
          k += " " + strArr[i];
  
      }
      System.out.print(k);
  
  }
  }
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>Exception in thread "main" java.util.NoSuchElementException
      at java.util.Scanner.throwFor(Unknown Source)
      at java.util.Scanner.next(Unknown Source)
      at ScannerPractice.main(ScannerPractice.java:28)
  </code></pre>
  
  <p>The exception refers to line 28, which is:</p>
  
  <pre><code>word.next();
  </code></pre>
  
  <p>I have tried looking at my for loop that assigns values to the string array, but I still couldn't find the error.</p>
  
  <p>I am racking my brain trying to solve this. Even a hint would be most appreciated.</p>