Keyboard input

Below program is an example on how to input from a keyboard(standard input)

import java.io.*;
class KeyboardInput{
public static void main(String args[])throws Exception{

//System.out is standard output
//System.err is standard error output
//System.in is standard input stream
//InputStreamReader r=new InputStreamReader(new FileInputStream(“../abc.txt”));
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);

//System.in.read(); //reads only one character(at a time) from keyboard

String name=””;

//! is Logical NOT operator
while(!name.equals(“exit”)){
System.out.println(“Enter data: “);
name=br.readLine();
System.out.println(“data is: “+name);
}

br.close();
r.close();
}
}

One thought on “Keyboard input”

Leave a Reply