Java Scanner Class: Managing User Input, File Data, and Regex Parses
🚀 What Will You Learn in This Guide?
In this guide, we will use the Java Scanner class to receive data from the user,
You will learn to process file contents and parse text with regular expressions (regex).
You will be able to use Scanner like an expert with practical examples, common errors and performance tips.
🧠 What is Scanner Class?
Scanner is a helper class included in the java.util package and shipped with Java version 1.5.
Its purpose is to convert data sources into primitive data types (int, double, String, etc.) by separating them into tokens.
The console reads and processes data from a file or fixed text source.
🔍 How Does Scanner Class Work?
The scanner splits the input data according to a defined separator pattern (default: space character).
Then, it reads and processes these parts sequentially with the next() method.
In short:
1️⃣ Defines the data as a source.
2️⃣ Sets the separator (with useDelimiter() if necessary).
3️⃣ Checks whether there is data with hasNext().
4️⃣ Reads with next() or nextLine().
5️⃣ Turns off the system resource with the close() method.
⚙️ Important Scanner Methods
| Method | Mission |
|---|---|
useDelimiter(String p) | It determines the pattern by which the data will be separated. |
hasNext() | Returns whether there is a new token to read. |
next() | Returns the next token. |
nextLine() | Reads the entire line. |
close() | Frees system resources by closing the scanner. |
🪜 How to Use Scanner Step by Step
1️⃣ Creating the Scanner Object
There are different constructors depending on the input you will use:
- Console →
new Scanner(System.in) - File →
new Scanner(new File("veri.txt")) - String →
new Scanner("örnek veri")
2️⃣ Setting Delimiter and Character Set
sc.useDelimiter(System.getProperty("line.separator"));
🧠 This code makes the line break a separator. It allows reading as a single token in blank entries.
3️⃣ Input Control and Reading
while (sc.hasNext()) {
System.out.println(sc.next());
}
🧠 hasNext() checks if there is data, next() gets the next entry.
4️⃣ Releasing Resources
sc.close();
🧠 If the scanner does not close, it will occupy system resources in vain.
💻 Usage Scenarios
🧍♂️ 1. Reading User Input
Scanner sc = new Scanner(System.in);
System.out.println("Adınızı ve soyadınızı girin:");
String ad = sc.next();
System.out.println("Merhaba " + ad);
sc.close();
🧠 Only the first word is read because Scanner sees the space as a separator.
🪶 2. Changing Separators for Blank Inputs
Scanner sc = new Scanner(System.in);
sc.useDelimiter(System.getProperty("line.separator"));
System.out.println("Adınızı ve soyadınızı girin:");
String tamAd = sc.next();
System.out.println("Merhaba " + tamAd);
sc.close();
🧠 With this method, the “Ayse Yilmaz” entry is read as one piece.
📁 3. Reading and Parsing File Data
package ornek.com.veri;
import java.io.*;
import java.util.*;
public class DosyaIsleyici {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("calisanlar.csv"));
sc.useDelimiter(System.getProperty("line.separator"));
List<Calisan> calisanlar = new ArrayList<>();
while (sc.hasNext()) {
calisanlar.add(ayracla(sc.next()));
}
sc.close();
System.out.println(calisanlar);
}
private static Calisan ayracla(String kayit) {
Scanner s = new Scanner(kayit);
s.useDelimiter(",");
Calisan c = new Calisan(s.nextInt(), s.next(), s.next());
s.close();
return c;
}
}
class Calisan {
int id; String ad; String pozisyon;
Calisan(int i, String a, String p){ id=i; ad=a; pozisyon=p; }
public String toString(){ return "Calisan[" + id + "," + ad + "," + pozisyon + "]"; }
}
📤 Output:
[Calisan[1,Jane Doe,CEO], Calisan[2,Mary Ann,CTO], Calisan[3,John Lee,CFO]]
🔢 4. Using Regex
String veri = "1a2b345c67d8,9#10";
Scanner sc1 = new Scanner(veri);
sc1.useDelimiter("\\D"); // Rakam olmayan her şeyi ayırıcı yapar
while (sc1.hasNext()) {
System.out.println(sc1.next());
}
sc1.close();
📤 Output:
1
2
345
67
8
9
10
💬 Frequently Asked Questions (FAQ)
- Why is the scanner known as “resource intensive”?
Each Scanner instance uses an explicit input stream. If the close() call that closes the file is not made, memory and file locks may occur.
- What is the difference between next() and nextLine()?
next() only reads up to the first space. nextLine() returns the entire line and moves to the next line.
- BufferedReader or Scanner?
BufferedReader is faster on large files. However, Scanner is more flexible in terms of data parsing (tokenization) and conversion to types.
- Is Scanner enough for CSV files?
For small CSV files yes. For large files, OpenCSV or java.nio.file is more suitable.
- What types of data can be read with Regex?
Data can be segmented according to numbers, letters, special characters or special patterns.
🏁 Conclusion
Java Scanner class is a powerful and practical solution to quickly parse user input, file data and text contents. It can be easily used in many scenarios thanks to advanced delimiters, regular expressions, and different constructors.
💡 Tip: Automate resource management in real projects by using the Scanner class with the try-with-resources structure.
☁️ You can increase the efficiency of your Java applications by developing and testing these techniques on the GenixNode platform.

