Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To expand on what Reimeus said: <code>Scanner.next()</code> returns the next <em>token</em> from the scanner. Basically, it regards whitespace as a delimiter between tokens meaning that "Marko Scekic" is two different tokens: "Marko" being the first, and "Scekic" being the second. </p> <p>You may be thinking of it like this: first the code does a System.out.println("What is your name?"); then asks the user for information using <code>next()</code> and then prints something else with <code>System.out.println()</code> so it seems that <code>next()</code> should give everything that was input between the two <code>println()</code> calls. </p> <p>Unfortunately, the Scanner class doesn't know anything at all about whether System.out.println() has been called or not. From its point of view the following are exactly the same: </p> <pre><code>String name; System.out.print("What is yout name?: "); name = user_input.next(); String quest; System.out.print("What is your quest?: "); quest = user_input.next(); </code></pre> <p>and</p> <pre><code>String name; name = user_input.next(); String quest; quest = user_input.next(); </code></pre> <p>In other words, printing things using <code>System.out.println()</code> has no effect on what <code>next()</code> does. </p> <p>So you need to either use something different than <code>Scanner</code>, or as Reimus said, use <code>nextLine()</code>, since this will return everything from the beginning of the line until the point that the user hits the enter key (which is a '\n' character).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload