Java - User Input
Interacting with users is a fundamental aspect of many applications, and Java provides libraries and techniques to efficiently collect and process user input. We will cover the following topics:
Reading User Input:
- Java provides the Scanner class to read user input from the console.
- Import the java.util.Scanner class at the beginning of your program.
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close();
}
}
Reading Different Data Types:
Handling various types of user input is a crucial skill for building interactive and versatile applications. We will cover methods for reading different data types and provide examples to illustrate each case.
Reading Strings
The nextLine() method of the Scanner class reads a line of text as a string.
import java.util.Scanner;
public class ReadStringInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close();
}
}
Reading Integers
The nexInt() method reads an integer from user input
import java.util.Scanner;
public class ReadIntInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("You are " + age + " years old.");
}
}
Reading Floating-Point Numbers
The nextDouble() method reads a floating-point number
import java.util.Scanner;
public class ReadDoubleInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
double number = scanner.nextDouble();
System.out.println("You entered: " + number);
}
}
Reading Characters
To read a single character, you can use next() and then extract the first character from the string.
import java.util.Scanner;
public class ReadCharInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character: ");
String input = scanner.next();
char firstChar = input.charAt(0);
System.out.println("You entered: " + firstChar);
}
}
Reading Boolean
To read a single character, you can For boolean values, you can read a string and parse it to obtain a boolean value.
import java.util.Scanner;
public class ReadBooleanInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter true or false: ");
String input = scanner.next();
boolean isTrue = Boolean.parseBoolean(input);
System.out.println("You entered: " + isTrue);
}
}
Handling User Input Mismatch
Input mismatch occurs when the user enters data that does not match the expected data type. It's essential to handle these exceptions gracefully to ensure the robustness of your Java programs.
Example of Input Mismatch Scenario
import java.util.Scanner;
public class MismatchExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
// If the user enters "abc", it causes an InputMismatchException.
}
}
Handling Input Mismatch with Try-Catch
To handle input mismatch exceptions, we use try-catch blocks.
import java.util.Scanner;
public class MismatchExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
try {
int age = scanner.nextInt();
System.out.println("You entered: " + age);
}
catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter an integer.");
}
}
}
Collecting and handling user input is a critical part of Java programming. The Scanner class simplifies this task by providing methods to read various data types from the console or files. Understanding how to read and process user input is essential for building interactive and user-friendly Java applications. Practice these techniques to improve your skills in user input handling.
Basic Topics
- Introduction to Java
- Data Types and Variables
- User Input
- Conditional Statements
- Loops
- Methods
- Arrays