
-----------------------------------
QuantumPhysics
Mon Sep 10, 2012 7:26 pm

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.

-----------------------------------
Insectoid
Mon Sep 10, 2012 7:55 pm

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.

-----------------------------------
QuantumPhysics
Tue Sep 11, 2012 7:34 am

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!

-----------------------------------
Aange10
Tue Sep 11, 2012 7:57 am

RE:Ask for user input on the same line
-----------------------------------
System.out.println("what command is used?");
input = user_input.nextInt(); 

I spy a potential error :)

-----------------------------------
QuantumPhysics
Tue Sep 11, 2012 11:44 am

RE:Ask for user input on the same line
-----------------------------------
Nope, no error

-----------------------------------
Aange10
Tue Sep 11, 2012 7:15 pm

RE:Ask for user input on the same line
-----------------------------------
Indeed. How about an error message?


java.util.InputMismatchException

