Daljeet12
Daljeet12

Reputation: 73

How to get user input in same line

I am trying to take input with print statement. How take input from the user in same place ?

package hello;

import java.util.Scanner;
public class hello {

    public static void main(String[] args) {

        int number;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter Number: ");

        number = sc.nextInt(); 

        sc.close();

        System.out.println("Number Entered: "+number);
    }
}

Program output

Enter Number: 
10
Number Entered: 10

I want to take input in first line

Enter Number:  10
Number Entered: 10

Upvotes: 1

Views: 1217

Answers (2)

Gourango Sutradhar
Gourango Sutradhar

Reputation: 1619

In Java System.out.println() means print a line after printing the passed string. Insted of println() call another function named print() it will just print the string without returning a new line. So, replace System.out.println("Enter Number: "); line into System.out.print("Enter Number: ");

Upvotes: 0

user2575725
user2575725

Reputation:

Just use print() instead of println().

System.out.print("Enter Number: ");

Upvotes: 2

Related Questions