java how to take user input code example
Example 1: how to take input in java
Scanner sc = new Scanner(System.in);
String userName = sc.nextLine();
int age = sc.nextInt();
long mobileNo = sc.nextLong();
double cgpa = sc.nextDouble();
System.out.println(userName);
Example 2: input java
Scanner in = new Scanner(System.in);
System.out.print("Please enter hour 1: ");
int hour1 = in.nextInt();
System.out.print("Please enter hour 2: ");
int hour2 = in.nextInt();
System.out.print("Please enter minute 1: ");
int min1 = in.nextInt();
System.out.print("Please enter minute 2: ");
int min2 = in.nextInt();
Example 3: user input in java
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}