Java skeneris (ar piemēriem)

Šajā apmācībā mēs ar piemēru palīdzību uzzināsim par Java skeneri un tā metodēm.

ScannerNo klases java.utilpaketē tiek izmantots, lai lasītu ievades datus no dažādiem avotiem, piemēram, ievades plūsmām, lietotājiem, failus, uc Paņemsim piemēru.

1. piemērs: lasiet teksta rindiņu, izmantojot skeneri

 import java.util.Scanner; class Main ( public static void main(String() args) ( // creates an object of Scanner Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); // takes input from the keyboard String name = input.nextLine(); // prints the name System.out.println("My name is " + name); // closes the scanner input.close(); ) )

Rezultāts

 Ievadiet savu vārdu: Kelvins. Mani sauc Kelvins

Iepriekš minētajā piemērā ievērojiet līniju

 Scanner input = new Scanner(System.in);

Šeit mēs esam izveidojuši objektu ar Scannernosaukumu input.

System.inParametrs tiek izmantots, lai ņemtu ievadi no standarta ievades. Tas darbojas tāpat kā ievades ievadīšana no tastatūras.

Pēc tam mēs izmantojām klases nextLine()metodi, Scannerlai lasītu lietotāja teksta rindiņu.

Tagad, kad jums ir ideja par to Scanner, izpētīsim vairāk par to.

Importēt skenera klasi

Kā redzams no iepriekš minētā piemēra, mums ir jāimportē java.util.Scannerpakete, pirms varam izmantot Scannerklasi.

 import java.util.Scanner;

Lai uzzinātu vairāk par pakotņu importēšanu, apmeklējiet Java paketes.

Izveidojiet skenera objektu Java

Pēc pakotnes importēšanas mēs varam izveidot Scannerobjektus.

 // read input from the input stream Scanner sc1 = new Scanner(InputStream input); // read input from files Scanner sc2 = new Scanner(File file); // read input from a string Scanner sc3 = new Scanner(String str);

Šeit mēs esam izveidojuši Scannerklases objektus, kas lasīs ievadi attiecīgi no InputStream, File un String.

Java skenera metodes ievades iegūšanai

ScannerKlase nodrošina dažādas metodes, kas ļauj mums, lai lasītu ieejas dažādu veidu.

Metode Apraksts
nextInt() nolasa intvērtību no lietotāja
nextFloat() nolasa floatvērtību veidlapu no lietotāja
nextBoolean() nolasa booleanvērtību no lietotāja
nextLine() nolasa lietotāja teksta rindiņu
next() nolasa lietotāja vārdu
nextByte() nolasa bytevērtību no lietotāja
nextDouble() doublno lietotāja nolasa e vērtību
nextShort() nolasa shortvērtību no lietotāja
nextLong() nolasa longvērtību no lietotāja

2. piemērs: Java skeneris nextInt ()

 import java.util.Scanner; class Main ( public static void main(String() args) ( // creates a Scanner object Scanner input = new Scanner(System.in); System.out.println("Enter an integer: "); // reads an int value int data1 = input.nextInt(); System.out.println("Using nextInt(): " + data1); input.close(); ) )

Rezultāts

 Ievadiet veselu skaitli: 22 Izmantojot nextInt (): 22

Iepriekš minētajā piemērā mēs izmantojām nextInt()metodi, lai nolasītu veselu skaitli.

3. piemērs: Java skeneris nextDouble ()

 import java.util.Scanner; class Main ( public static void main(String() args) ( // creates an object of Scanner Scanner input = new Scanner(System.in); System.out.print("Enter Double value: "); // reads the double value double value = input.nextDouble(); System.out.println("Using nextDouble(): " + value); input.close(); ) )

Rezultāts

 Ievadiet Double vērtību: 33.33 Izmantojot nextDouble (): 33.33

Iepriekš minētajā piemērā mēs izmantojām nextDouble()metodi, lai nolasītu peldošā komata vērtību.

4. piemērs: Nākamais Java skeneris ()

 import java.util.Scanner; class Main ( public static void main(String() args) ( // creates an object of Scanner Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); // reads the entire word String value = input.next(); System.out.println("Using next(): " + value); input.close(); ) )

Rezultāts

 Ievadiet savu vārdu: Jonny Walker Izmantojot nākamo (): Jonny

Iepriekš minētajā piemērā mēs izmantojām next()metodi, lai lasītu virkni no lietotāja.

Šeit mēs esam norādījuši pilnu vārdu. Tomēr next()metode nolasa tikai vārdu.

Tas ir tāpēc, ka next()metode nolasa ievadi līdz atstarpei . Tiklīdz tiek parādīta atstarpe , tā atgriež virkni (izņemot atstarpi).

5. piemērs: Java skeneris nextLine ()

 import java.util.Scanner; class Main ( public static void main(String() args) ( // creates an object of Scanner Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); // reads the entire line String value = input.nextLine(); System.out.println("Using nextLine(): " + value); input.close(); ) )

Rezultāts

 Ievadiet savu vārdu: Jonny Walker Izmantojot nextLine (): Jonny Walker

Pirmajā piemērā mēs izmantojām nextLine()metodi, lai lasītu virkni no lietotāja.

Atšķirībā next()no nextLine()metodes metode nolasa visu ievades līniju, ieskaitot atstarpes. Metode tiek pārtraukta, kad tā sastopas ar nākamo rindas rakstzīmi .

Recommended Reading: Java Scanner skipping the nextLine().

Java Scanner with BigInteger and BigDecimal

Java scanner can also be used to read the big integer and big decimal numbers.

  • nextBigInteger() - reads the big integer value from the user
  • nextBigDecimal() - reads the big decimal value from the user

Example 4: Read BigInteger and BigDecimal

 import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; class Main ( public static void main(String() args) ( // creates an object of Scanner Scanner input = new Scanner(System.in); System.out.print("Enter a big integer: "); // reads the big integer BigInteger value1 = input.nextBigInteger(); System.out.println("Using nextBigInteger(): " + value1); System.out.print("Enter a big decimal: "); // reads the big decimal BigDecimal value2 = input.nextBigDecimal(); System.out.println("Using nextBigDecimal(): " + value2); input.close(); ) )

Output

 Enter a big integer: 987654321 Using nextBigInteger(): 987654321 Enter a big decimal: 9.55555 Using nextBigDecimal(): 9.55555

In the above example, we have used the java.math.BigInteger and java.math.BigDecimal package to read BigInteger and BigDecimal respectively.

Working of Java Scanner

ScannerKlase skan visu līniju un sadala auklu marķieriem. Žetoni ir mazi elementi, kuriem ir kāda nozīme Java kompilatoram. Piemēram,

Pieņemsim, ka ir ievades virkne:

 He is 22

Šajā gadījumā skenera objekts nolasīs visu rindu un sadalīs virkni marķieros: " Viņš ", " ir " un " 22 ". Tad objekts atkārto katru marķieri un nolasa katru marķieri, izmantojot dažādas metodes.

Piezīme . Pēc noklusējuma atstarpes tiek izmantotas žetonu sadalīšanai.

Interesanti raksti...