Méthode Java.util.Scanner.hasNext()
Qu'est-ce que la méthode hasNext() en Java ?
Le hasNext() est une méthode de la classe java.util.Scanner, qui renvoie true si ce scanner a un autre jeton dans son entrée.
La méthode hasNext() peut être distinguée sur la base du paramètre passé tel que
1) java.util.Scanner hasNext () //Aucun paramètre passé
Cette méthode de classe java.util.Scanner renvoie true si ce scanner a un autre jeton dans son entrée. Il peut bloquer l'attente d'une entrée à numériser car le scanner n'avance pas au-delà d'une entrée.
2) java.util.Scanner hasNext (String_Pattern) //Paramètre de modèle de chaîne passé
Cette méthode de classe java.util.Scanner renvoie true si le jeton suivant correspond au modèle d'une chaîne spécifiée
3) java.util.Scanner hasNext (Pattern_Pattern) //Paramètre de modèle de modèle passé
Cette méthode de classe java.util.Scanner renvoie true si le jeton suivant correspond au modèle spécifié.
Syntaxe
Pour aucun paramètre
public boolean hasNext()
Pour le paramètre de modèle de chaîne
public boolean hasNext(String_Pattern)
Pour le paramètre de modèle de modèle
public boolean hasNext(Pattern_Pattern)
Paramètres :
Paramètre | Type | Description |
String_Pattern | Chaîne | chaîne contenant le motif à scanner |
Pattern_Pattern | Modèle | motif à analyser pour toute chaîne spécifiée |
Valeur de retour
Le Java.util.Scanner.hasNext() Méthode méthode renvoie TRUE si le scanner a un autre jeton dans son entrée.
Exception
La fonction hasNext() renvoie exception IllegalStateException
si au cas où le scanner est fermé
Programme de démonstration de l'exception IllegalStateException
//Java program to illustrate the IllegalStateException in hasNext Function
//Importing the Scanner Class of Util Package
import java.util.*;
//Main Class of the program
public class Main {
//Main method of the program
public static void main(String args[]){
//String Variable
String str = "STechies - Online Tutorials";
//Scanner Class object
Scanner scanner = new Scanner(str);
//Checking scanner's next token matches the String pattern
//Scanner object closed before innovation
scanner.close();
System.out.println("Match Found : "+scanner.hasNext("Stechies"));
}
}
SORTIE
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.hasNext(Scanner.java:1433)
at java.util.Scanner.hasNext(Scanner.java:1400)
at Main.main(Main.java:11)
Exemple de hasNext() en Java
Programme 1 :java.util.Scanner hasNext ()
//Java code for hasNew Function
//Importing the Scanner Class of Util Package
import java.util.Scanner;
//Main Class of the program
public class Main {
//Main Method of the program
public static void main (String[] args) {
//String variable
String s = "STechies - Online Tutorials";
//Creating the object of Scanner Class
Scanner scan = new Scanner(s);
//Printing the output
System.out.println(scan.hasNext());
}
}
SORTIE :
true
Programme 2 :java.util.Scanner hasNext (String_Pattern)
//Java code for hasNew Function with pattern
//Importing the Scanner Class of Util Package
import java.util.Scanner;
//Main Class of the program
public class Main {
//Main Method of the program
public static void main (String[] args) {
//String variable
String string = "STechies - Online Tutorials";
//Creating the object of Scanner Class
Scanner scan = new Scanner(string);
String pattern = "STechies.*";
if(scan.hasNext(pattern)){
System.out.println("pattern matched");
}else{
System.out.println("Unmatched");
}
}
}
SORTIE :
pattern matched
Programme 3 :java.util.Scanner hasNext (Pattern_Pattern)
//Java program to illustrate the working of hasNext String pattern
//Importing the Scanner Class of Util Package
import java.util.*;
//Main Class of the program
public class Main {
//Main Method of the program
public static void main(String args[]){
//String Variable
String str = "STechies - Online Tutorials";
//Scanner Class object
Scanner scanner = new Scanner(str);
//Checking scanner's next token matches the String pattern
System.out.println("Match Found : "+scanner.hasNext("Stechies"));
}
}
SORTIE :
Match Found : false