Dalam Java, Scanner ialah kelas yang digunakan untuk mendapatkan input rentetan dan jenis primitif yang berbeza seperti int, double, dll. Kelas pengimbas ditemui dalam pakej java. Ia memanjangkan Objek kelas dan melaksanakan antara muka Boleh Ditutup dan Iterator. Input dipecahkan kepada kelas dengan bantuan pembatas ruang putih. Pengisytiharan, sintaks, kerja dan beberapa aspek lain dalam kelas Pengimbas di Java akan dibincangkan dalam bahagian berikut.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Pengisytiharan Kelas Pengimbas Java
Kelas Pengimbas Java diisytiharkan seperti yang ditunjukkan di bawah.
public final class Scanner extends Object implements Iterator
Salin selepas log masuk
Sintaks:
Kelas pengimbas boleh digunakan dalam sintaks di bawah dalam program Java.
Scanner sc = new Scanner(System.in);
Salin selepas log masuk
Di sini, sc ialah objek pengimbas dan System.in memberitahu Java bahawa inputnya ialah ini.
Bagaimana Kelas Pengimbas Java Berfungsi?
Sekarang, mari kita lihat cara kelas Pengimbas berfungsi.
- Import kelas java.util.Scanner atau keseluruhan pakej java.util.
import java.util.*; import java.util.Scanner;
Salin selepas log masuk
- Buat objek pengimbas seperti yang ditunjukkan dalam sintaks.
Scanner s = new Scanner(System.in);
Salin selepas log masuk
- Isytiharkan jenis int, aksara atau rentetan.
int n = sc.nextInt();
Salin selepas log masuk
- Lakukan operasi pada input berdasarkan keperluan.
Pembina
Kelas pengimbas di Java mempunyai pembina yang berbeza. Mereka ialah:
- Pengimbas(Fail src):Pengimbas baharu akan dibina yang menjana nilai daripada fail yang disebutkan.
- Pengimbas(Fail src, String charsetName):Pengimbas baharu akan dibina yang menjana nilai yang diimbas daripada fail yang disebutkan.
- Pengimbas(InputStream src):Pengimbas baharu akan dibina yang menjana nilai daripada aliran input yang disebutkan.
- Pengimbas(InputStream src, String charsetName):Pengimbas baharu akan dibina yang menjana nilai daripada strim input yang disebutkan.
- Pengimbas(Path src):Pengimbas baharu akan dibina yang menjana nilai daripada fail yang disebutkan.
- Pengimbas(Path src, String charsetName):Pengimbas baharu akan dibina yang menjana nilai daripada fail yang disebutkan.
- Pengimbas(Src boleh dibaca):Pengimbas baharu akan dibina yang menjana nilai daripada sumber yang disebutkan.
- Pengimbas(ReadableByteChannel src):Pengimbas baharu akan dibina yang menjana nilai daripada saluran yang disebutkan.
- Pengimbas(ReadableByteChannel src, String charsetName):Pengimbas baharu akan dibina yang menjana nilai daripada saluran yang disebut.
- Pengimbas(String src):Pengimbas baharu akan dibina yang menjana nilai daripada rentetan yang disebutkan.
Kaedah Kelas Pengimbas Java
Berikut ialah kaedah yang melaksanakan fungsi yang berbeza.
- close():Scanner gets closed on calling this method.
- findInLine(Pattern p):Next occurrence of the mentioned pattern p will be attempted to find out, ignoring the delimiters.
- findInLine(String p):The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
- delimiter():Pattern that is currently used by the scanner to match delimiters will be returned.
- findInLine(String p):The next occurrence of the pattern that is made from the string will be attempted to find out, ignoring the delimiters.
- findWithinHorizon(Pattern p, int horizon):Tries to identify the mentioned pattern’s next occurrence.
- hasNext():If the scanner has another token in the input, true will be returned.
- findWithinHorizon(String p, int horizon):Tries to identify the next occurrence of the mentioned pattern made from the string p, ignoring delimiters.
- hasNext(Pattern p):If the next token matches the mentioned pattern p, true will be returned.
- hasNext(String p):If the next token matches the mentioned pattern p made from the string p, true will be returned.
- next(Pattern p):Next token will be returned, which matches the mentioned pattern p.
- next(String p):Next token will be returned, which matches the mentioned pattern p made from the string p.
- nextBigDecimal():Input’s next token will be scanned as a BigDecimal.
- nextBigInteger():Input’s next token will be scanned as a BigInteger.
- nextBigInteger(int rad):Input’s next token will be scanned as a BigInteger.
- nextBoolean():Input’s next token will be scanned as a Boolean, and it will be returned.
- nextByte():Input’s next token will be scanned as a byte.
- nextByte(int rad):Input’s next token will be scanned as a byte.
- nextDouble():Input’s next token will be scanned as a double.
- nextFloat():Input’s next token will be scanned as a float.
- nextInt():Input’s next token will be scanned as an integer.
- nextInt(int rad):Input’s next token will be scanned as an integer.
- nextLong(int rad):Input’s next token will be scanned as a long.
- nextLong():Input’s next token will be scanned as a long.
- nextShort(int rad):Input’s next token will be scanned as a short.
- nextShort():Input’s next token will be scanned as a short.
- radix():The default radix of the scanner will be returned.
- useDelimiter(Pattern p):Delimiting pattern of the scanner will be set to the mentioned pattern.
- useDelimiter(String p):Delimiting pattern of the scanner will be set to the mentioned pattern made from the string p.
- useRadix(int rad):The scanner’s default radix will be set to the mentioned radix.
- useLocale(Locale loc):The locale of the scanner will be set to the mentioned locale.
Examples of Java Scanner Class
Now, let us see some sample programs of the Scanner class in Java.
Example #1
Code:
import java.util.Scanner; class ScannerExample { public static void main(String[] args) { //create object of scanner Scanner sc = new Scanner(System.in); System.out.println("Enter the username"); String obj = sc.nextLine(); System.out.println("The name you entered is: " + obj); } }
Salin selepas log masuk
Output:
![Kelas Pengimbas Java](https://img.php.cn/upload/article/000/000/000/172500541199692.png)
On executing the program, the user will be asked to enter a name. As the input is a string, the nextLine() method will be used. Once it is entered, a line will be printed displaying the name user given as input.
Example #2
Code:
import java.util.Scanner; class ScannerExample { public static void main(String[] args) { //create object of scanner Scanner sc = new Scanner(System.in); System.out.println("Enter age"); int obj = sc.nextInt(); System.out.println("The age you entered is: " + obj); } }
Salin selepas log masuk
Output:
![Kelas Pengimbas Java](https://img.php.cn/upload/article/000/000/000/172500541323064.png)
On executing the program, the user will be asked to enter the age. As the input is a number, the nextInt() method will be used. Once it is entered, a line will be printed displaying the age user given as input.
Atas ialah kandungan terperinci Kelas Pengimbas Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!