Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

You haven't posted the whole code. But, change

System.out.println("Original price of item: $");

to

System.out.print("Original price of item: $");

and it will be ok.

It's because the System.out.println() method adds a newline character to everything it prints out. If you want the prompt for input to remain on the same line, use System.out.print() (notice that's the print() method, not println()) which does not add a newline to what it sends to standard out. System.out is a static java.io.PrintStream object. You can read the Javadocs on it to help you out here : http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html

Because you used println instead of print. The 'ln' adds a new line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top