Friday, June 10, 2016

java - How to print a line and type input in the same line




I want to print a line like this:



Result: [the result are input here and after click on Enter to continue]



How can I do that?



EDIT:




This is what I want:



Scanner user1 = new Scanner(System.in);

int x = user1.nextInt();

System.out.println("Result: "+x);



But the last line won't print unless I type my input and press Enter.


Answer



This is a very old thread but since there doesn't seem to be an accepted answer, I am answering. Hope it would help someone else...
Here is what you are looking for



Scanner input = new Scanner (System.in);
System.out.print("Enter a number : ");
int num = input.nextInt();
System.out.println("Hello you entered "+num);



Note that when you use println the cursor moves to next line.
If you use just print the next statement continues on the same line.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...