Input from Keyboard (Scanner Class) in Java
Overview
Taking input from the keyboard is a common requirement in many Java applications. The Scanner
class, available in the java.util
package, provides a simple way to read input from the keyboard. This tutorial covers the basics of using the Scanner
class to read different types of data from the user.
Key Points:
- The
Scanner
class is used to read input from various sources, including the keyboard. - You need to import the
java.util.Scanner
package to use theScanner
class. - The
nextLine()
,nextInt()
,nextDouble()
, and other methods are used to read different types of data.
Importing the Scanner Class
To use the Scanner
class, you need to import it from the java.util
package.
Example:
import java.util.Scanner;
Creating a Scanner Object
After importing the Scanner
class, you can create a Scanner
object to read input from the keyboard.
Syntax:
Scanner scanner = new Scanner(System.in);
Example:
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Reading Different Types of Data
The Scanner
class provides various methods to read different types of data from the keyboard:
nextLine()
: Reads a string of text.nextInt()
: Reads an integer.nextDouble()
: Reads a double.nextBoolean()
: Reads a boolean.
Example:
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your height (in cm):");
double height = scanner.nextDouble();
System.out.println("Are you a student? (true/false):");
boolean isStudent = scanner.nextBoolean();
System.out.println("You entered - Age: " + age + ", Height: " + height + " cm, Student: " + isStudent);
}
}
Handling Input Errors
When reading input from the keyboard, you may encounter input errors if the user enters data of the wrong type. To handle such errors, you can use try-catch blocks.
Example:
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("You entered: " + age);
} catch (Exception e) {
System.out.println("Invalid input. Please enter an integer.");
}
}
}
Closing the Scanner
It is a good practice to close the Scanner
object after you are done using it to free up system resources.
Example:
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
// Close the scanner
scanner.close();
}
}
Summary
In this tutorial, you learned how to use the Scanner
class to read input from the keyboard in Java. The Scanner
class provides a simple way to read different types of data, including strings, integers, doubles, and booleans. You also learned how to handle input errors and the importance of closing the Scanner
object. Properly handling user input is essential for building interactive Java applications.