Lee Kole
3
Q:

how to get a user input in java

import java.util.Scanner;  // Import the Scanner class

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output user input
  }
}
23
// import Scanner
import java.util.Scanner;

// Initialize Scanner
Scanner input = new Scanner(System.in);

// Test program with Scanner
System.out.println("What is your name?");
String name = input.nextLine();

System.out.println("Hello," + name + " , it is nice to meet you!");
4
Scanner sc = new Scanner(System.in);  // Create a Scanner object
String userName = sc.nextLine();//read input string
int age = sc.nextInt(); //read input integer
long mobileNo = sc.nextLong(); //read input long
double cgpa = sc.nextDouble(); //read input double
System.out.println(userName);//output 
18

New to Communities?

Join the community