Program for taking Input & Output in Java

Description


Input is any information that is needed by your program to complete its execution.  There are many forms that program input may take.  Some programs use graphical components like a popup dialog box to accept and return the character string that is typed by the user.

Output is any information that the program must convey to the user.  The information you see on your computer screen is being output by one or more programs that are currently running on your computer.  When you decide to print a document, a program is told to send some output to the printer.  Any sound that your computer makes is because some program sent output to the speakers on your computer.  The possibilities for program output are also limited only by our imaginations.

 

In JAVA there are multiple ways of taking Input & showing Output to User…!!!

They are :

S. No. Classes Imports
1. BufferedReader java.io.*
2. Scanner java.util.*
An Example Code of Scanner Class for taking Input/Output in Java
1. Create a Scanner using the InputStream available.
Scanner scanner = new Scanner( System.in );

2. Don’t forget to prompt the user
System.out.print( “Type some data for the program: ” );

3. Use the Scanner to read a line of text from the user.
String input = scanner.nextLine();

4. Now, you can do anything with the input string that you need to.
Like, output it to the user.
System.out.println( “input = ” + input );

An Example Code of BufferedReader Class for taking Input/Output in Java
1. Create a Scanner using the InputStream available.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

2. Don’t forget to prompt the user
System.out.print( “Type some data for the program: ” );

3. Use the Scanner to read a line of text from the user.
String input = br.readLine();
OR for Integer
int number = Integer.parseInt(br.radLine());
& same as for other Data Types..!!

4. Now, you can do anything with the input string that you need to.
Like, output it to the user.
System.out.println( “input = ” + input );

NOTE: For BufferedReader Class, you must throw an IOException..!! –> Just like this : “throws IOException”

SOURCE CODE (BufferedReader Class)
OUTPUT (BufferedReader Class)

Author Description

humanz

Our mission is to bring the affordable Technology education to people everywhere, in order to help them achieve their dreams and change the world. - Follow @program_guide

No comments yet.

Join the Conversation