Tuesday, March 10, 2009

Keyboard Input with Java

Sometimes, we need to write programs that would apply to real life scenarios. I have modified the JAVA program to accept more type of inputs.

Click the link to view the source code.

[+/-] show/hide

/* Keyboard input *using BUfferedREader/InputStreamReader*/

public class KeyboardInput{
publi static void main(string args[])
throws Exception{
double rate=120;
double hours;
String name;
InputStreamReader i = new InputStreamReader(;
BufferedREader bufread=new BufferedReader( i );
System.out.print("\nInput your name: ");
String name=bufread.readLine();
System.out.print("\nInput your hours worked: ");
String shours=bufread.readLine();
hours=Double.parseDouble(shours); /*to convert a string to double data type*/
System.out.print("\nEmployee : "+name);
System.out.println("\nSalary: "+compute(rate,hours)); /*invoking method compute*/

public static double compute(double r,double h)
{return r* h;}