In java we can read input values in 6 ways:
- Scanner class: present in java.util.*; package and it has many methods, based your input types you can utilize those methods. a. nextInt() b. nextLong() c. nextFloat() d. nextDouble() e. next() f. nextLine(); etc...
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a :");
int a = sc.nextInt();
System.out.println("Enter b :");
int b = sc.nextInt();
int c = a + b;
System.out.println("Result: "+c);
}
}
- BufferedReader class: present in java.io.*; package & it has many method, to read the value from the keyboard use "readLine()" : this method reading one line at a time.
import java.io.BufferedReader;
import java.io.*;
public class MyClass {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new BufferedReader(new InputStreamReader(System.in)));
System.out.println("Enter a :");
int a = Integer.parseInt(br.readLine());
System.out.println("Enter b :");
int b = Integer.parseInt(br.readLine());
int c = a + b;
System.out.println("Result: "+c);
}
}