logo

Méthode Java Scanner next()

next() est une méthode de la classe Java Scanner qui recherche et renvoie le prochain jeton complet du scanner utilisé. Il existe trois types différents de méthode Java Scanner next() qui peuvent être différenciés en fonction de leur paramètre. Ceux-ci sont:

  • Méthode Java Scanner next()
  • Méthode Java Scanner next (modèle de chaîne)
  • Méthode Java Scanner next (modèle de modèle)

1. Méthode Java Scanner next()

Il s'agit d'une méthode de classe Scanner utilisée pour obtenir le prochain jeton complet du scanner utilisé. Un jeton complet est précédé et suivi d'une entrée qui correspond au modèle de délimiteur.

2. Méthode Java Scanner next (modèle de chaîne)

Il s'agit d'une méthode de classe Scanner qui renvoie le jeton suivant s'il correspond au modèle construit à partir de la chaîne spécifiée.

3. Méthode Java Scanner next (modèle de modèle)

Il s'agit d'une méthode de classe Scanner qui renvoie le jeton suivant s'il correspond au modèle spécifié.

Syntaxe

Voici les déclarations de suivant() méthode:

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

Paramètre

Type de données Paramètre Description Obligatoire/Facultatif
Chaîne modèle C'est une chaîne spécifiant le modèle à analyser. Requis
Modèle modèle C'est le modèle à rechercher pour la chaîne spécifiée. Requis

Retour

La méthode next() renvoie les prochains jetons complets.

Des exceptions

NoSuchElementException - Cette exception sera levée si aucun autre jeton n'est trouvé.

IllegalStateException - Cette exception sera levée si l'invocation est effectuée après la fermeture du scanner.

Version de compatibilité

Java 1.5 et supérieur

Exemple 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(); } } 

Sortir:

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

Exemple 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(); } } 

Sortir:

 hasNextLine public boolean hasNextLine() IllegalStateException 

Exemple 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(); } } 

Sortir:

 Token Value1 Facebook.com Token value2: JavaTpoint.com 

Exemple 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(); } } 

Sortir:

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

Exemple 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(); } } 

Sortir:

 JavaTpoint Hello