Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using scanner to get input not printing as expected

This is for a friend of mine who is having trouble with Java for school.

I do know some programming, but not Java.

Scanner kbReader = new Scanner(System.in);
System.out.print("Name of item: ");
String name = kbReader.next();
System.out.println("Original price of item: $");
double price = kbReader.nextDouble();

Outputs:

Name of item: Coat
Original price of item: $

10

Why is the input for "Original price of item: $" on the next line? I was guessing it was because I went from String to double, but can't think of a different way to do it?

like image 884
user Avatar asked Nov 26 '25 23:11

user


1 Answers

If you use

System.out.println()

Java will put a newline after the output, if you use

System.out.print()

Java won't put a newline after the output.

like image 133
rael_kid Avatar answered Nov 28 '25 12:11

rael_kid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!