read input from user java 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: 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 3: read input in java
3 ways to read input from console in java
-----------------------------
1. Using BufferedReader Class
2. Using Scanner Class
3. Using Console Class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
...
public static void main(String[] args) throws IOException
...
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
import java.util.Scanner;
...
String name = new Scanner(System.in);
String name = Syste.console().readLine();