input number java code example
Example 1: 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 2: java get input
Scanner sc = new Scanner(System.in);
String s = sc.next();
int n = sc.nextInt();
double d = sc.nextDouble();
float f = sc.nextFloat();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int c = br.read();
Example 3: how to get user input in java
import java.util.*;
class UserInputDemo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}
Example 4: output statement java
System.out.println("Hello World");