title: How to split a string with space being the delimiter using Scanner
body: <p>I am trying to split the input sentence based on space between the words. It is not working as expected. </p>
  
  <pre><code>public static void main(String[] args) {
      Scanner scaninput=new Scanner(System.in);
      String inputSentence = scaninput.next();
      String[] result=inputSentence.split("-");
      // for(String iter:result) {
      //     System.out.println("iter:"+iter);
      // }
      System.out.println("result.length: "+result.length);
      for (int count=0;count&lt;result.length;count++) {
          System.out.println("==");
          System.out.println(result[count]);
      }
  }
  </code></pre>
  
  <p>It gives the output below when I use "-" in split:</p>
  
  <pre><code>fsfdsfsd-second-third
  result.length: 3
  ==
  fsfdsfsd
  ==
  second
  ==
  third
  </code></pre>
  
  <p>When I replace "-" with space " ", it gives the below output. </p>
  
  <pre><code>first second third
  result.length: 1
  ==
  first
  </code></pre>
  
  <p>Any suggestions as to what is the problem here? I have already referred to the stackoverflow post <a href="https://stackoverflow.com/questions/7899525/how-to-split-a-string-by-space">How to split a String by space</a>, but it does not work.</p>
  
  <p>Using <code>split("\\s+")</code> gives this output:</p>
  
  <pre><code>first second third
  result.length: 1
  ==
  first
  </code></pre>