31 Facts to Understand Input/ Output in Java

Java / Friday, August 2nd, 2019

Input/ Output in Java

Here in this post we will know 31 Fact to Understand Input/ Output in Java.

1. During input, data flows through the following five objects:

  • System.in
  • reader
  • input
  • data object (Name etc.), and
  • System.out

2. System.in and System.out are drawn as objects of the System class.

3. The System class is contained in the java.io package, which is to be imported for input/ output operation.

4. The three objects – reader, input and data are to be defined in the program.

5. The reader object is treated as an instance of the InputStreamReader class.

6. input is created as an object of the BufferedReader class.

7. The reader object acts as a pipeline between the keyboard and the input stream System.in. It is then transmitted into the program.

8. The readLine( ) method of the BufferedReader class enables the object input to extract input in a convenient way.

9. The readLine( ) method enables reading an entire line of character, entered through the keyboard.

10. When the readLine( ) method is called, the program waits till the information is entered by the user.

11. The information is passed to the program in the form of a string object.

12. In the java.io package, nearly fifty I/O classed exist, out of which three are very much required. They are:

  • InputStream class
  • InputStreamReader class extends to Reader class, and
  • BufferedReader class

13. These three classes may be included in a program by the following statement: import java.io.*;

14. These may be also be included for use in a program by their qualified names, such as

  • import java.io.InputStream;
  • import java.io.InputStreamReader;
  • import java.io.BufferedReader;

15. The java.lang package, which is automatically imported in all programs, has the System class. It spontaneously creates an InputStream object, as a System.in object and is in turn used to create, connected to the keyboard.

16. This InputStream object is used to produce an instance of the BufferedReader class.

17. An instance of the InputStreamReader class is used to create an object of the BufferedReader object.
InputStreamReader indata = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(indata);

18. Steps of inputs by user through console:

Step 1: InputStreamReader isr = new InputStreamReader(System.in);
// System.in object helps create an InputStreamReader object.

Step 2: BufferedReader br = new BufferedReader(br);
// InputStreamReader object helps create BufferedReader object.

Step 3: System.out.println(“Enter data: “);
// A prompt asking the user to enter data is displayed on screen.

Step 4: String input = br.readLine( );
/* The BufferedReader object helps in reading a line of text entered by the user */

19. The readLine( ) method and BufferedReader return only String objects.

20. The String object may be passed (converted) and then stored as requisite primitive datatype.
For example,
String input = br.readLine(  );
double number = Double.parseDouble(input);

21. As an object of the Reader class helps input string to work, there is a Printer class whose object enables the output stream to function.

22. The PrintStream class extends the Printer class.

23. System.out is an object of the PrintStream class.

24. The print( ) and println( ) methods of the Printer class are mostly useful in producing output on the computer screen.

25. The Scanner class was a gift of Java 1.5 (JDK 5.0) version.

26. This class has made the keyboard simpler.

27. The Scanner class enables having input from the user.

28. The format of using the Scanner class:

import java.util.Scanner;
public class <class_name>{
Scanner userInput = new Scanner(System.in);

29. The Scanner class has quite a few methods for the next element of input.

30. The method nextLine( ) gets lines of inputs one after another.

31. The method next( ) gets the next whitespace delimiter token.

 Example Program 01

Write a program to calculate the volume of a cylinder. The radius and height are to be taken from the user.

// author Rajib Kumar Saha

import java.io.*;

public class CylinderVol{
	public static void main(String args[]) throws IOException{
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		double radius, height, volume;

		System.out.print("Enter radius of the cylinder: ");
		radius = Double.parseDouble(br.readLine());
		System.out.print("Enter height of the cylinder: ");
		height = Double.parseDouble(br.readLine());

		volume = Math.PI * Math.pow(radius, 2) * height;

		System.out.println("The volume of the cylinder is "+volume);
 Example Program 02

Write a program, using the Scanner class, to find the bill amount in a restaurant charging 12.5% tax on bills.

// author Rajib Kumar Saha

import java.util.Scanner;

public class RestaurantBill{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		double amt, tax;

		System.out.print("Enter the total food amount: ");
		amt = sc.nextDouble();

		tax = (amt * 12.5) / 100;

		System.out.println("Adding 12.5% service charge Bill amounts to "+ (amt + tax));


<< Previous     Next>>


Leave a Reply

Your email address will not be published.