Computer Science Canada

Ask for user input on the same line

Author:  QuantumPhysics [ Mon Sep 10, 2012 7:26 pm ]
Post subject:  Ask for user input on the same line

Okay well, you guys were right, Java is easy is to learn if you are fluent with other languages. Anyways, the year just started and all but I just want to ask one question that's been bothering me quite a bit. I am using the Eclipse Indigo IDE. The question I want to know is after I prompt the user to input a line during run-time it asks for input on the next line as so:

Please enter a number:
5 (user entered number)

How can I change it so that it asks for input next to the end of the line and not below the statement like here:

Please enter a number: 5

I know with C/C++,Python,COBOL and FASM this is default but, well of course java having all its stupid malfunctions this annoys me... How do I ask for input on the same like what would i change

int input;
Scanner user_input = new Scanner(System.in);
System.out.println("what command is used?");
input = user_input.nextInt();

Any help would be great! Thanks.

Author:  Insectoid [ Mon Sep 10, 2012 7:55 pm ]
Post subject:  RE:Ask for user input on the same line

[Simply replace System.out.println with System.out.print. Print does not add a newline, println does.

Author:  QuantumPhysics [ Tue Sep 11, 2012 7:34 am ]
Post subject:  RE:Ask for user input on the same line

Oh lol never even thought of that. My teacher said that the only way to output text is to type system.out.println. Thanks Insectoid, that worked well!

Author:  Aange10 [ Tue Sep 11, 2012 7:57 am ]
Post subject:  RE:Ask for user input on the same line

Java:
System.out.println("what command is used?");
input = user_input.nextInt();


I spy a potential error Smile

Author:  QuantumPhysics [ Tue Sep 11, 2012 11:44 am ]
Post subject:  RE:Ask for user input on the same line

Nope, no error

Author:  Aange10 [ Tue Sep 11, 2012 7:15 pm ]
Post subject:  RE:Ask for user input on the same line

Indeed. How about an error message?

Eclipse wrote:

java.util.InputMismatchException


: