title: Java Scanner - Read line breaks in to a string?
body: <p>I have a scanner that takes user input until ctrl+d and then a while loop which adds each word to a string and then prints it, but i'd like to know how to also include the new line indicators like \n in the string wherever there is a new line.</p>
  
  <pre><code>Scanner sc = new Scanner(System.in);
  System.out.println("Enter your string: ");
  String x = "";
  
  while (sc.hasNext()){
    x+=sc.next() +" ";
  }
  System.out.println(x);
  </code></pre>
  
  <p>E.g this code would take the input:</p>
  
  <pre><code>Hello
  Hello
  Hello
  </code></pre>
  
  <p>and print: Hello Hello Hello</p>
  
  <p>Whereas I would like it to print something like:</p>
  
  <pre><code>Hello\n Hello\n Hello\n
  </code></pre>
  
  <p>so I can see where each line ends.</p>