Reading Run Time Input in Java

 There are many way to take input from keyboard.

a) Scanner Class
b) Console Class
c) DataInputStream Class 
d)  BufferedReader Class
e) System.in
f) InputStreamReader

a) Scanner Class : import.util.Scanner 

create Scanner Class object to use methods of scanner class 
Scanner sc = new Scanner (System.in)
      or reading form file or a string
for file  File text = new File( "C:/aa.txt");
Scanner sc = new Scanner ( text);

 int a = sc.nextInt(); for int value 
float a = sc.nextFloat();
double a = sc.nextDouble();
String a = sc.next();

or many methods to related to expression

sc.nextLine(); skip line 
sc.hasNext();
sc.findInLine("string" ); find a pattern in a line
sc.findWithinHorizion("stinddd" , 10);
  
b) Console Class  : import.java.io.Console
 Console con = System.console();
String name = con.readLine();


c) DataInputStream  text = DataInputStream(System.io);
String aa = text.readLine();
int a =  Integer.parseInt(aa);


d) BufferedReader text = new BufferedReader ( new InputStreamReader(System.in);
String aa = text.readLine();
int a =  Integer.parseInt(aa);

e)  Reading char from input 
     char c = (char) System.in.read();

f) InputStreamReader aa = new InputStreamReader(Systeem.in);
     char c = (char) aa..read();








Comments

Popular posts from this blog

Creational Design Pattern: Factory Design Pattern

Introduction to Java

Design Patterns: The Secret Weapon of Expert Developers