logo

Java Scanner next() metoda

The next() je metoda klase Java Scanner koja pronalazi i vraća sljedeći potpuni token iz skenera koji se koristi. Postoje tri različite vrste Java Scanner next() metode koje se mogu razlikovati ovisno o parametru. Ovi su:

  • Java Scanner next() metoda
  • Java Scanner next(String pattern) Metoda
  • Java Scanner next(Pattern pattern) Metoda

1. Java Scanner next() metoda

To je metoda klase skenera koja se koristi za dobivanje sljedećeg potpunog tokena od skenera koji se koristi. Kompletnom tokenu prethodi i slijedi unos koji odgovara uzorku razdjelnika.

2. Java Scanner next(String pattern) Metoda

To je metoda klase Scanner koja vraća sljedeći token ako se podudara s uzorkom konstruiranim iz navedenog niza.

3. Java Scanner next (Pattern pattern) Metoda

To je metoda klase Scanner koja vraća sljedeći token ako odgovara navedenom uzorku.

Sintaksa

Slijede izjave od Sljedeći() metoda:

 public String next() public String next(String pattern) public String next(Pattern pattern) 

Parametar

Tip podataka Parametar Opis Obavezno/opcionalno
Niz uzorak To je niz koji navodi uzorak za skeniranje. Potreban
Uzorak uzorak To je obrazac za skeniranje navedenog niza. Potreban

Povratak

Metoda next() vraća sljedeće potpune žetone.

Iznimke

NoSuchElementException - Izbacit će ovu iznimku ako se ne pronađe više tokena.

IllegalStateException - Izbacit će ovu iznimku ako se pozivanje izvrši nakon zatvaranja Scannera.

Verzija kompatibilnosti

Java 1.5 i novije

Primjer 1

 import java.util.*; public class ScannerNextExample1 { public static void main(String[] args) { System.out.print('Enter full name: '); //Create scanner object and read the value from the console Scanner scan = new Scanner(System.in); //Read the first token String firstName = scan.next(); //Read the second token String lastName = scan.next(); //Print the token values read by Scanner object System.out.println('First Name is: '+firstName); System.out.println('Last Name is: '+lastName); scan.close(); } } 

Izlaz:

 Enter full name: Hritik Roshan First Name is: Hritik Last Name is: Roshan 

Primjer 2

 import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class ScannerNextExample2 { public static void main(String args[]) throws FileNotFoundException{ //Declare File object File file = new File('/home/javatpoint/Desktop/ABHISHEK/AngularJS/Index/abc.txt'); //Initialize the scanner Scanner scan = new Scanner(file); // iterate through the file line by line while(scan.hasNextLine()){ //Print the contents of a file by line System.out.println(scan.next()); } scan.close(); } } 

Izlaz:

 hasNextLine public boolean hasNextLine() IllegalStateException 

Primjer 3

 import java.util.*; public class ScannerNextExample3 { public static void main(String args[]) { String s = 'Facebook.com 
 JavaTpoint.com 22 60.0'; //Create a new scanner with the specified String Object Scanner scanner = new Scanner(s); //Find the next token and print it System.out.print('Token Value1 ' + scanner.next()); System.out.print('
Token value2: ' + scanner.next()); scanner.close(); } } 

Izlaz:

 Token Value1 Facebook.com Token value2: JavaTpoint.com 

Primjer 4

 import java.util.*; public class ScannerNextExample4 { public static void main(String args[]) { //Initialize Scanner object Scanner scan = new Scanner('22 313 45 87'); //Intialize the String pattern String pattern = '[0-9]*'; //Print the tokenized Strings while(scan.hasNext()){ System.out.println('tokenized Strings: '+scan.next(pattern)); } scan.close(); } } 

Izlaz:

 tokenized Strings: 22 tokenized Strings: 313 tokenized Strings: 45 tokenized Strings: 87 

Primjer 5

 import java.util.*; import java.util.regex.Pattern; public class ScannerNextExample5 { public static void main(String args[]){ String str = 'JavaTpoint Hello World!'; Scanner scanner = new Scanner(str); //Check if next token matches the pattern and print it System.out.println('' + scanner.next(Pattern.compile('.....point'))); //Check if next token matches the pattern and print it System.out.println('' + scanner.next(Pattern.compile('..llo'))); scanner.close(); } } 

Izlaz:

 JavaTpoint Hello