Input get in java
There is two different way to get the input in java. Scanner and BufferedReader.
1. Scanner
Scanner is used to get the input in a runtime.It's buffer memory size is 1KB.
Example-1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner object = new Scanner(System.in); int a = object.nextInt(); System.out.print(a); } }
Output
Input :9 Output :9
Another data types Getting
Data type | Scanner |
---|---|
int | object.nextInt() |
float | object.nextFloat() |
String | object.nextLine() |
char | object.next().charAt(0); |
double | object.nextDouble() |
long | object.nextLong() |
2. BufferedReader
BufferedReader is used to get the input in a runtime. It's buffer memory size is 8KB. It is faster than scanner.
Example-1
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); //get integer int a=Integer.parseInt(read.readLine()); System.out.println(a); //get string String s=read.readLine(); System.out.println(s); } }
Output
Input :9 Output :9 Input :Books Output :Books
Another data types Getting
Data type | BufferedReader |
---|---|
int | Integer.parseInt(read.readLine()); |
float | Float.parseFloat(read.readLine()); |
String | read.readLine(); |
double | Double.parseDouble(read.readLine()); |
long | Long.parseLong(read.readLine()); |