Metodo scanner nextLine() in Java con esempi
IL riga successiva() metodo di java.util.Scanner classe fa avanzare questo scanner oltre la riga corrente e restituisce l'input che è stato saltato. Questa funzione stampa il resto della riga corrente, tralasciando il separatore di riga alla fine. Il successivo è impostato dopo il separatore di riga. Poiché questo metodo continua a cercare nell'input alla ricerca di un separatore di riga, potrebbe cercare in tutto l'input cercando la riga da saltare se non sono presenti separatori di riga.
Sintassi:
public String nextLine()
parametri: La funzione non accetta alcun parametro.
Valore di ritorno: Questo metodo restituisce il file linea quello è stato saltato
Eccezioni: La funzione genera due eccezioni come descritto di seguito:
- NoSuchElementException: genera se non è stata trovata alcuna riga IllegalStateException: genera se questo scanner è chiuso
I seguenti programmi illustrano la funzione di cui sopra:
Programma 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();> > }> }> |
Produzione:
Gfg Geeks GeeksForGeeks
Programma 2: Per dimostrare 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);> > }> > }> }> |
Produzione:
Exception thrown: java.util.NoSuchElementException: No line found
Programma 3: Per dimostrare 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);> > }> > }> }> |
Produzione:
Exception thrown: java.lang.IllegalStateException: Scanner closed
Riferimento: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()