Méthode Scanner nextLine() en Java avec exemples

Le ligne suivante() méthode de java.util.Scanner La classe avance ce scanner au-delà de la ligne actuelle et renvoie l'entrée qui a été ignorée. Cette fonction imprime le reste de la ligne actuelle, en laissant de côté le séparateur de ligne à la fin. Le suivant est placé après le séparateur de ligne. Étant donné que cette méthode continue de rechercher dans l'entrée à la recherche d'un séparateur de ligne, elle peut rechercher dans toutes les entrées la ligne à sauter si aucun séparateur de ligne n'est présent.

Syntaxe:

public String nextLine() 

Paramètres: La fonction n'accepte aucun paramètre.

Valeur de retour : Cette méthode renvoie le doubler cela a été ignoré

Des exceptions: La fonction lève deux exceptions comme décrit ci-dessous :

    NoSuchElementException : lance si aucune ligne n'a été trouvée. IllegalStateException : lance si ce scanner est fermé

Les programmes ci-dessous illustrent la fonction ci-dessus :

Programme 1 :




// Java program to illustrate the> // nextLine() method of Scanner class in Java> // without parameter> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> > throws> Exception> > {> > > String s => 'Gfg Geeks GeeksForGeeks'> ;> > > // create a new scanner> > // with the specified String Object> > Scanner scanner => new> Scanner(s);> > > // print the next line> > System.out.println(scanner.nextLine());> > > // print the next line again> > System.out.println(scanner.nextLine());> > > // print the next line again> > System.out.println(scanner.nextLine());> > > scanner.close();> > }> }>

Sortir:

 Gfg Geeks GeeksForGeeks 

Programme 2 : Pour démontrer NoSuchElementException




// Java program to illustrate the> // nextLine() method of Scanner class in Java> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> > throws> Exception> > {> > > try> {> > > String s => ''> ;> > > // create a new scanner> > // with the specified String Object> > Scanner scanner => new> Scanner(s);> > > System.out.println(scanner.nextLine());> > scanner.close();> > }> > catch> (Exception e) {> > System.out.println(> 'Exception thrown: '> + e);> > }> > }> }>

Sortir:

 Exception thrown: java.util.NoSuchElementException: No line found 

Programme 3 : Pour démontrer IllegalStateException




// Java program to illustrate the> // nextLine() method of Scanner class in Java> // without parameter> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> > throws> Exception> > {> > > try> {> > > String s => 'Gfg'> ;> > > // create a new scanner> > // with the specified String Object> > Scanner scanner => new> Scanner(s);> > > scanner.close();> > > // Prints the new line> > System.out.println(scanner.nextLine());> > scanner.close();> > }> > catch> (Exception e) {> > System.out.println(> 'Exception thrown: '> + e);> > }> > }> }>

Sortir:

 Exception thrown: java.lang.IllegalStateException: Scanner closed 

Référence: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()