Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Ask for user input on the same line
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
QuantumPhysics




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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.
QuantumPhysics




PostPosted: 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!
Aange10




PostPosted: 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
QuantumPhysics




PostPosted: Tue Sep 11, 2012 11:44 am   Post subject: RE:Ask for user input on the same line

Nope, no error
Aange10




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: