Standard Console Input Java

Standard Console Input Java; Java supports user input in three ways of from command line, keyboard and GUI.
Following Example is on Standard Console Input Java takes input from User
import java.io.*;
public class ConsoleInput
{
 public static void main(String args[]) throws IOException
 {
   InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);

   System.out.println("Enter Your Employer Name:");
   String name = br.readLine();

   System.out.println("Enter Your Basic Salary:");
   String salary1 = br.readLine();
   int salary2 = Integer.parseInt(salary1);

   System.out.println("Enter Your House Rent:");
   String rent1 = br.readLine();
   double rent2 = Double.parseDouble(rent1);

   System.out.println("Your name is " + name);
   System.out.println("Your Total Salary is " + (salary2+rent2));
 }
}

Standard Console Input JavaOutput Screenshot on Standard Console Input Java

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.in implicitly gets connected to input mechanism of OS. BufferedReader connects to System.in through InputStreamReader class. readLine() reads keyboard input and returns as a string. Even numbers like basic salary and house rent are returned as strings. The strings are to be parsed to numbers to be used in arithmetic operations.

More programs are available on the following links.

1. Byte streams – With DataInputStream
2 Character streams – With BufferedReader
3. java.util class – Scanner

The last one is easy to write as no parsing is required, but the client should have at least JDK 1.5 version.

Leave a Comment

Your email address will not be published.