This project demonstrates how to handle user input in Java using the Scanner class to read and display a password from the keyboard.
- Initialize the
Scannerobject for reading fromSystem.in. - Read exactly one line of text using the
nextLine()method. - Store the input in a
Stringvariable. - Print the entered password to the console for confirmation.
- Language: Java 23 (Current version).
- IDE: IntelliJ IDEA.
- Concepts: Standard Input (
System.in),Scannerclass, String Handling.
This task introduces the Scanner utility, which acts as a bridge between the user's keyboard and the program. By using nextLine(), we ensure that the entire sequence of characters (including spaces) is captured until the user presses Enter.
- Interactivity: It allows the program to process dynamic data provided at runtime instead of hardcoded values.
- Standard Practice: Using
ScannerwithSystem.inis the fundamental way to build console applications in Java. - Robustness:
nextLine()is safer thannext()because it reads the full line, preventing issues with trailing newline characters in more complex input scenarios.
Enter your password:
qwerty123
Your password is: qwerty123
package com.yurii.pavlenko;
import java.util.Scanner;
/**
* Task 20: Simple Login Input.
* Demonstrates reading user input from the console using the Scanner class.
*/
public class Task_20_App {
public static void main(String[] args) {
// 1. Create a Scanner object to read input from the keyboard (System.in)
Scanner scanner = new Scanner(System.in);
// 2. Prompt the user to enter a single line as a password
// and read the input into the password variable.
System.out.println("Enter your password:");
String password = scanner.nextLine();
// 3. Output the entered password to the screen
System.out.println("Your password is: ");
System.out.println(password);
// 4. Close the scanner to free up system resources.
scanner.close();
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT