Java User Input (Scanner)
Let the learner provide a value.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind User Input (Scanner).
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
Scanner can parse tokens or read complete lines from an input source. This browser editor uses supplied STDIN for a run. The example checks hasNextLine and uses Kavi when input is empty, so it can also run without waiting for interactive typing.
Explore the details
Java User Input
The Scanner class is used to get
user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.
In our example, we will use the nextLine() method, which is used to read Strings:
If you don't know what a package is, read our Java Packages Tutorial.
Input Types
In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at the table below:
In the example below, we use different methods to read data of various types:
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine(); // reads a String
System.out.print("Enter your age: ");
int age = sc.nextInt(); // reads an int
System.out.print("Enter your GPA: ");
double gpa = sc.nextDouble(); // reads a double
System.out.println("Hi " + name + "! Age: " + age + ", GPA: " + gpa);
sc.close();
// Sample output: Hi Alice! Age: 20, GPA: 3.8Note: If you enter wrong input (e.g. text in a numerical input), you will get an exception/error message (like "InputMismatchException"). You can read more about exceptions and how to handle errors in the Exceptions chapter.
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String name = scanner.hasNextLine() ? scanner.nextLine() : "Kavi";
System.out.println("Hello, " + name + "!");
}
}Predict first. Then reveal the expected output / வெளியீடு
Hello, Kavi!
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Open the editor I/O or STDIN panel, supply a name, and run again.
Reveal the answer and reasoning / விடை
The greeting uses the supplied input line. Empty input produces the default greeting shown above.
One mistake to avoid: nextInt reads a token, while nextLine reads the rest of a line. After mixing them, account for the remaining line separator.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.