Say I had the Scanner method, and I want to print the user input after that.
Take this code for instance:
String randomWords;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter two words separated: ");
randomWords = kb.next();
System.out.println(randomWords);
And if the two separated words entered were
hello world
Only
hello
is printed
Why is this? and how can I print both of the words with the space included?
Thank you.
Use Scanner#nextLine() instead,
Scanner#next() reads the next complete token basing on the delimiter.
As default delimiter of the scanner is whitespace, you should explicitly define the delimiter for your scanner using Scanner#useDelimiter(str).
If you use
\nnext line as delimiter your curretn code would work.