sparkles sparkles - 1 year ago 68
Java Question

scanner records string with spaces and numbers?

I'm taking user input via a scanner that needs to record something like "Gallon 2% milk."
Right now I have

System.out.print("Enter product description: ");
String description =;

now this works fine for things like "milk" but when I enter "gallon 2% milk" it returns an error.

also, I've tried using

String description = in.nextLine();

but that prints "Enter product description: Enter product quantity:"

I'm assuming that is because the next line in my code is

System.out.print("Enter product quantity: ");

thanks in advance for the help!

Answer Source

if have a prompt before System.out.print("Enter product description: "); and you are pressing the ENTER key before this then that keystroke will also need to be consumed used nextLine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download