title: How do you keep scanner.next() from including newline?
body: <p>I am trying to simply read words in a text file using scanner.next() with delimiter equal " " but the scanner includes the newline/carriage return with the token.</p>
  
  <p>I have scoured the internet trying to find a good example of this problem and have not found it so I am posting it here. I can't find another similar problem posted here on SO. I also looked over the documentation on scanner and pattern (<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html" rel="nofollow">http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html</a>) but I still cannot find a way to solve this.</p>
  
  <p><strong>Text file:</strong></p>
  
  <p>This is a test</p>
  
  <p>to see if1 this, is working</p>
  
  <p>ok!</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>int i = 0;
  String string;
  try(Scanner scanner = new Scanner(new File(filename))) {
      scanner.useDelimiter(" ");
      while(scanner.hasNext())
      {
      string = scanner.next();
      System.out.println(i++ + ": " + string);
      }
  }catch(IOException io_error) {
      System.out.println(io_error);
      }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <p>0: This</p>
  
  <p>1: is</p>
  
  <p>2: a</p>
  
  <p>3: test</p>
  
  <p>to</p>
  
  <p>4: see</p>
  
  <p>5: if1</p>
  
  <p>6: this,</p>
  
  <p>7: is</p>
  
  <p>8: working</p>
  
  <p>ok!</p>
  
  <p>As you can see, #3 and #8 have two words separated by a newline. (I know I can separate these into two separate strings.)</p>