Chapter 11: JAVA Input Methods
Introduction
In programming, obtaining input from users is a critical task. Java offers various ways to capture
input, whether it's through the command line, graphical user interfaces, or more advanced methods.
Understanding how to use these input methods will allow you to create interactive and dynamic programs.
Lesson Objectives
• Knowledge: Understand different Java input methods, including Scanner, BufferedReader, and
JOptionPane.
• Skill: Demonstrate the ability to implement various input methods in a Java program.
• Affective: Appreciate the importance of user input in making programs interactive and user-
friendly.
Discussion of the Lesson
Java provides several ways to gather input from users, ranging from basic command-line interfaces to
graphical input dialogs. The most common methods are:
• Scanner: A simple and commonly used class for getting input from the user through the console.
• BufferedReader: A more efficient method for reading text from an input stream.
• JOptionPane: A class used to create standard dialog boxes that can gather user input through a
graphical interface.
Each method has its strengths and is suited for different types of applications.
Example 1: Using Scanner
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + ". You are " + age + " years old.");
scanner.close();
}
}
29
Java Design & Development: A Guide to Object-Oriented Programming
Halner D. Alejaga, LPT
Importing the Scanner Class
The import statement is used to include the Scanner class from the java.util package, allowing the
program to use it for reading user input.
Defining the Class
This line defines a public class named ScannerExample. In Java, every executable program must have at
least one class with a main method.
The main Method
The main method is the entry point of any Java program. The code inside this method is executed when
the program runs.
Creating a Scanner Object
This line creates a Scanner object named scanner that is linked to System.in, which represents the
standard input stream (usually the keyboard). The Scanner object will be used to capture user input.
Prompting the User for Their Name
• System.out.print("Enter your name: "); displays a prompt asking the user to enter their name.
• String name = scanner.nextLine(); reads the entire line of text input by the user (up to the
newline character) and stores it in the name variable as a String.
Prompting the User for Their Age
• System.out.print("Enter your age: "); displays a prompt asking the user to enter their age.
• int age = scanner.nextInt(); reads the next integer input by the user and stores it in the age
variable as an int. This assumes the user will input a valid integer.
Displaying the User's Information
This line uses System.out.println() to print a message that combines the name and age variables with a
greeting. It constructs the message by concatenating strings and variables.
Closing the Scanner
Finally, scanner.close(); closes the Scanner object. This is good practice as it frees up system resources
associated with the Scanner. Although it's not strictly necessary for a small program like this, it's a good
habit to close resources when you're done with them.
30
Java Design & Development: A Guide to Object-Oriented Programming
Halner D. Alejaga, LPT