Introduce Java User Input (Scanner) and give examples
In Java, the Scanner
class is a part of the java.util
package and is used to obtain input from various sources, including user input from the console. It provides methods to read different types of data, such as strings, integers, and floating-point numbers.
java.util.Scanner
package at the beginning of your Java program.Scanner
object to read input.Scanner
object when you're done to free up resources.Here’s a simple example demonstrating how to use the Scanner
class to read user input:
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
// Create a Scanner object
Scanner scanner = new Scanner(System.in);
// Prompt the user for their name
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // Read a string input
// Prompt the user for their age
System.out.print("Enter your age: ");
int age = scanner.nextInt(); // Read an integer input
// Prompt the user for their height
System.out.print("Enter your height in meters: ");
double height = scanner.nextDouble(); // Read a double input
// Display the collected information
System.out.println("Hello, " + name + "!");
System.out.println("You are " + age + " years old.");
System.out.println("Your height is " + height + " meters.");
// Close the scanner
scanner.close();
}
}
import java.util.Scanner;
imports the Scanner
class.Scanner scanner = new Scanner(System.in);
creates a new Scanner
object that reads from the standard input (keyboard).scanner.nextLine()
reads a line of text (string).scanner.nextInt()
reads an integer.scanner.nextDouble()
reads a double (floating-point number).scanner.close();
is called to close the Scanner
object, which is a good practice to prevent resource leaks.The Scanner
class provides several other methods to read different types of input:
next()
: Reads the next token (word) as a string.nextBoolean()
: Reads the next token as a boolean.nextByte()
: Reads the next token as a byte.nextShort()
: Reads the next token as a short.nextLong()
: Reads the next token as a long.nextFloat()
: Reads the next token as a float.Here’s another example that demonstrates reading multiple inputs in a loop:
import java.util.Scanner;
public class MultipleInputsExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("How many numbers do you want to enter? ");
int count = scanner.nextInt();
int sum = 0;
for (int i = 0; i < count; i++) {
System.out.print("Enter number " + (i + 1) + ": ");
int number = scanner.nextInt();
sum += number; // Add the number to the sum
}
System.out.println("The sum of the entered numbers is: " + sum);
scanner.close();
}
}
In this example, the program asks the user how many numbers they want to enter, reads that many integers, and calculates their sum.
The Scanner
class is a powerful and flexible way to handle user input in Java. By using it, you can easily read various types of data from the console, making it a fundamental tool for interactive Java applications.