A simple example to illustrate how java.util.Scanner works would be reading a single integer from System.in. It's really quite simple.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

To retrieve a username I would probably use sc.nextLine().

System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);

You could also use next(String pattern) if you want more control over the input, or just validate the username variable.

You'll find more information on their implementation in the API Documentation for java.util.Scanner

Answer from Rune Vikestad on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_user_input.asp
Java User Input (Scanner class)
import java.util.Scanner; // Import the Scanner class class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = myObj.nextLine(); ...
Discussions

How to read strings from a Scanner in a Java console application? - Stack Overflow
By this, I mean that the scanner reads the input: "firstname lastname" as two separate tokens. So in your example, ename would be set to firstname and the scanner is attempting to set the supervisorId to lastname ... scanner.nextLine(); //This is needed to pick up the new line ; System.out.println("Enter EmployeeName:"); ename=scanner.nextLine(); 2013-07-17T05:08:05.333Z+00:00 ... You are entering a null value to nextInt, it will fail if you give a null value... ... import java... More on stackoverflow.com
🌐 stackoverflow.com
java - How to capture enter key, using scanner as console input? - Stack Overflow
I want to capture enter character in console. I am inputting 2 strings. Case 1. removestudent(pressing enter) removes all the students from array list. Case 2. removestudent student1 removes More on stackoverflow.com
🌐 stackoverflow.com
Why is it printing the same thing twice?
keyboard.next() is only returning the next word in your input each time you call it, because its tokenizing the input on the space chars. So your while loop is executing one time for each word you type, not one time for the whole line like you are thinking, so the println statement is printing one line for each word it sees. You need to use "keyboard.nextLine()" to get the whole line at once, execute the loop only once per input line, and get your text printed back on one line like you're expecting. More on reddit.com
🌐 r/java
4
2
September 20, 2012
Can't type user input for Java in Vs Code?

use code runner extension and add "code-runner.runInTerminal": true in vs code settings.

More on reddit.com
🌐 r/vscode
3
5
August 9, 2017
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Scanner-User-Input-example-String-next-int-long-char
Java Scanner User Input Example
Use the Scanner’s next () or nextLine() methods to convert user input into the appropriate type. Use the Java user input in your program.
🌐
Medium
medium.com › @AlexanderObregon › a-beginners-guide-to-handling-user-input-in-java-with-scanner-85be33c8702d
A Beginner’s Guide to Handling User Input in Java with Scanner
March 18, 2024 - To begin using the Scanner class, you must first import it at the beginning of your Java code. This is done with the import statement: ... Once imported, you can create an instance of the Scanner class. The most common use-case for beginners is to read input from the keyboard, which is done by passing System.in (the standard input stream) to the Scanner constructor: ... This line of code creates a new Scanner object named scanner that is ready to read input from the console.
🌐
Java67
java67.com › 2012 › 12 › how-to-read-user-input-from-console-in-java.html
How to read User Input from Console in Java? Scanner Example | Java67
/** * * Java program to read input ... args[]) { //Creating Scanner instance to scan console for User input Scanner console = new Scanner(System.in); System.out.println("System is ready to accept input, please enter name : "); String ...
🌐
Baeldung
baeldung.com › home › java › java io › read and write user input in java
Read and Write User Input in Java | Baeldung
January 8, 2024 - Finally, we’ll see how to use the Console class, available since Java 6, for both console input and output. For our first examples, we’ll use the Scanner class in the java.util package to obtain the input from System.in — the “standard” input stream:
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-user-input-scanner-class
Java User Input - Scanner Class - GeeksforGeeks
The Scanner class, introduced in Java 5, belongs to the java.util package allows developers to read input from different sources easily. The Scanner class can read input from keyboard (console), files, strings, and data streams.
Published   January 16, 2026
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › java – how to read input from console using scanner
Java - How to read input from console using Scanner - Mkyong.com
January 31, 2020 - package com.mkyong.io; import java.util.Scanner; public class JavaScanner { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { System.out.print("Please enter your name: "); String input = scanner.nextLine(); System.out.println("name : " + input); System.out.print("Please enter your age: "); int age = scanner.nextInt(); System.out.println("age : " + age); } } }
🌐
TutorialsPoint
tutorialspoint.com › ways-to-read-input-from-console-in-java
Ways to read input from console in Java
July 9, 2020 - import java.util.Scanner; public class Demo{ public static void main(String args[]){ Scanner my_scan = new Scanner(System.in); String my_str = my_scan.nextLine(); System.out.println("The string is "+my_str); int my_val = my_scan.nextInt(); System.out.println("The integer is "+my_val); float my_float = my_scan.nextFloat(); System.out.println("The float value is "+my_float); } }
🌐
DataFlair
data-flair.training › blogs › read-java-console-input
How to Read Java Console Input | 3 Ways To Read Java Input - DataFlair
April 19, 2022 - It provides useful methods like nextInt(), nextDouble(), nextFloat(), etc, for parsing primitive data types from tokenized input. Also it uses regular expressions which we can use to find tokens.
🌐
LabEx
labex.io › tutorials › java-how-to-use-the-scanner-class-for-console-input-in-java-414170
How to use the Scanner class for console input in Java | LabEx
To use the Scanner class in your Java program, you need to import it from the java.util package. Add the following line at the top of your Java file: ... Once you have imported the Scanner class, you can create a Scanner object to read input from the console.
🌐
UTK
web.eecs.utk.edu › ~bvanderz › cs365 › examples › datacheck.html
Using Java's Scanner and Console Class to Read Input
Hence you must wrap a Scanner object around System.in to handle string oriented IO. The following statement accomplishes this task: Scanner console = new Scanner(System.in); If you want to treat System.in directly as a character stream, use the Console class instead, which is discussed below.
🌐
CodeJava
codejava.net › java-se › file-io › 3-ways-for-reading-input-from-the-user-in-the-console
3 ways for reading user's input from console in Java
July 27, 2019 - Scanner scanner = new Scanner(System.in); System.out.print("Enter your nationality: "); String nationality = scanner.nextLine(); System.out.print("Enter your age: "); int age = scanner.nextInt();Advantages: Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input. Regular expressions can be used to find tokens. ... The Console class was introduced in Java 1.6, and it has been becoming a preferred way for reading user’s input from the command line.
🌐
Index.dev
index.dev › blog › java-capture-user-input-console
Best Ways to Capture User Input in Java (With Examples)
February 14, 2025 - If you just need a simple solution, Scanner works well. For handling large text inputs efficiently, BufferedReader is the way to go. And if you need secure input (like passwords), Console is your best bet. Older options like DataInputStream still exist, but they’re mostly outdated. Knowing these choices—and their pitfalls—helps you write cleaner, faster, and more secure code. Java keeps evolving, so staying up to date with best practices ensures your apps stay reliable and scalable.
🌐
Blogger
javarevisited.blogspot.com › 2012 › 12 › how-to-read-input-from-command-line-in-java.html
How to read input from command line in Java using Scanner - Example
August 19, 2021 - Java 5 introduced a nice utility called java.util.Scanner is capable of reading input from the command line in Java. Using Scanner is a nice and clean way of retrieving user input from the console or command line.
🌐
Sentry
sentry.io › sentry answers › java › how to get user input in java
How To Get User Input in Java | Sentry
For example, the following code uses Scanner with methods such as nextLine() to retrieve different data inputs: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); // Read a full line of text System.out.println("Hello, " + name + "!"); System.out.print("Enter your age: "); int age = scanner.nextInt(); // Read an integer input System.out.println("You are " + age + " years old."); System.out.print("Enter a decimal number: "); double number = scanner.nextDouble(); // Read a double input System.out.println("You entered: " + number); scanner.close(); // Close the scanner to free resources } }
🌐
LinkedIn
linkedin.com › pulse › how-read-user-input-from-console-java-beginners-guide
How to Read User Input from the Console in Java: A Beginner's Guide
March 4, 2023 - The Scanner class is a class in ... read input from the console, we first need to create an instance of the Scanner class and then call the appropriate method to read the data....
🌐
Intellipaat
intellipaat.com › home › blog › java user input – scanner, bufferedreader and console
Java User Input - Scanner, BufferedReader and Console - Intellipaat Blog
February 20, 2026 - The Scanner class is easy and reads the input from different data types such as integers, doubles, and strings. The BufferedReader class is faster and efficient for large inputs, but it requires manual conversion as throughput only comes in ...