logo

Escàner mètode nextLine() a Java amb exemples

El nextLine() mètode de java.util.Scanner class fa avançar aquest escàner més enllà de la línia actual i retorna l'entrada que s'ha omès. Aquesta funció imprimeix la resta de la línia actual, deixant de banda el separador de línies al final. El següent s'estableix després del separador de línies. Com que aquest mètode continua cercant a través de l'entrada buscant un separador de línies, pot cercar totes les entrades cercant la línia per saltar-se si no hi ha cap separador de línies.

Sintaxi:



public String nextLine()>

Paràmetres: La funció no accepta cap paràmetre.

Valor de retorn: Aquest mètode retorna el línia que es va saltar

Excepcions: La funció llança dues excepcions tal com es descriu a continuació:



    NoSuchElementException: llança si no s'ha trobat cap línia IllegalStateException: llança si aquest escàner està tancat

Els programes següents il·lustren la funció anterior:

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

ordenació d'inserció en java

>

>

Sortida:

 Gfg Geeks GeeksForGeeks>

Programa 2: Per demostrar NoSuchElementException




vlc baixar vídeos de youtube
// 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);> >}> >}> }>

>

>

Sortida:

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

Programa 3: Per demostrar 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);> >}> >}> }>

mètodes de llista de matrius
>

>

Sortida:

 Exception thrown: java.lang.IllegalStateException: Scanner closed>

Referència: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()