Kristian Kristian - 1 year ago 98
Java Question

Scanner only reads first word instead of line

In my current program one method asks the user to enter the description of a product as a

input. However, when I later attempt to print out this information, only the first word of the
shows. What could be the cause of this? My method is as follows:

void setDescription(Product aProduct) {
Scanner input = new Scanner(;
System.out.print("Describe the product: ");
String productDescription =;
aProduct.description = productDescription;

So if the user input is "Sparkling soda with orange flavor", the
will only yield "Sparkling".

Any help will be greatly appreciated!

Answer Source

Replace next() with nextLine():

String productDescription = input.nextLine();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download