Java User Input
Let the learner provide a value.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- 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.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
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.
விரிவான விளக்கங்கள்
Java User Input
User-கிட்ட இருந்து input பெறதுக்கு Scanner class use பண்றோம்.
முதல்ல java.util.Scanner-ஐ import பண்ணணும்:
Different input types-க்கு different Scanner methods:
| Method | Description |
|---|---|
| nextBoolean() | boolean value படிக்கும் |
| nextByte() | byte value படிக்கும் |
| nextDouble() | double value படிக்கும் |
| nextFloat() | float value படிக்கும் |
| nextInt() | int value படிக்கும் |
| nextLine() | String value படிக்கும் |
| nextLong() | long value படிக்கும் |
| nextShort() | short value படிக்கும் |
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hi " + name + "! Age: " + age);
sc.close();`Scanner` class-ஐ use முடிந்ததும் என்ன செய்ய வேண்டும்?
- எதுவும் செய்ய வேண்டாம்
- scanner.close() call செய்ய வேண்டும்
- System.exit() call செய்ய வேண்டும்
- scanner.reset() call செய்ய வேண்டும்
Reveal answer / விடை
scanner.close() call செய்ய வேண்டும்
`scanner.close()` அல்லது try-with-resources பயன்படுத்துவது best practice. Resource leak avoid செய்யும். `try (Scanner sc = new Scanner(System.in)) { … }` — automatically close ஆகும்.
முழுமையான உதாரணம்
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.
நீங்களே முயற்சி செய்யுங்கள்
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.
தவிர்க்க வேண்டிய தவறு: nextInt reads a token, while nextLine reads the rest of a line. After mixing them, account for the remaining line separator.
பாடத்தின் சுருக்கம்
- 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.