Java user input and output
So far, we have focused only on one sided Java concepts - that is, no user input is accepted. Accepting user input is a great way to allow users to interact with a program.
This lesson focuses on:
- Importing the necessary package for user input
- Using the necessary classes for user input
- Handling the exceptions
Importing the necessary package for user input
The package that needs to be imported to accept user input is java.io. The java.io package contains classes and interfaces used for input and output.
Example:
import java.io.*;
class GetUserInput{
}
Using the necessary classes for user input
To get user input, use the BufferedReader and InputStreamReader classes.-
The BufferedReader class
The BufferedReader class buffers the user's input to make it work more efficiently.
-
The InputStreamReader class
The InputStreamReader class reads the user's input.
Example:
class GetUserInput{
public static void main(String[] args){
//the data that will be entered by the user
String name;
//an instance of the BufferedReader class
//will be used to read the data
BufferedReader reader;
//specify the reader variable
//to be a standard input buffer
reader = new BufferedReader(new InputStreamReader
(System.in));
//ask the user for their name
System.out.println("What is your name?");
//read the data entered by the user using
//the readLine() method of the BufferedReader class
//and store the value in the name variable
name = reader.readLine();
}
}
Handling the exceptions
Using the above code, you still cannot accept user input because one thing is still missing, and that is you have to catch the exception(s).
What if the code does not work properly? What if the user does not enter any data? Such situations are called exceptions, and in programs that accept user input, you need a way to handle them. The exception we will be using is the IOException. This exception is used when an input error occurs. Exceptions are coverted in detail in the next lesson. But for now, you should know that exceptions are handled with something called a try-catch block which is basically a block of code that will execute one set of code if there is no error and another set of code if there is an error.
Example:
class GetUserInput{
public static void main(String[] args){
//the data that will be entered by the user
String name;
//an instance of the BufferedReader class
//will be used to read the data
BufferedReader reader;
//specify the reader variable
//to be a standard input buffer
reader = new BufferedReader(new InputStreamReader
(System.in));
//ask the user for their name
System.out.println("What is your name?");
try{
//read the data entered by the user using
//the readLine() method of the BufferedReader class
//and store the value in the name variable
name = reader.readLine();
//print the data entered by the user
System.out.println("Your name is " + name);
}
catch (IOException ioe){
//statement to execute
//if an input/output error occurs
System.out.println("An unexpected
error occured.");
}
}
}




