logo

Método Scanner nextLine() en Java con ejemplos

El Proxima linea() método de java.util.Escáner La clase avanza este escáner más allá de la línea actual y devuelve la entrada que se omitió. Esta función imprime el resto de la línea actual, omitiendo el separador de línea al final. El siguiente se establece después del separador de línea. Dado que este método continúa buscando a través de la entrada buscando un separador de línea, puede buscar en toda la entrada buscando la línea a omitir si no hay separadores de línea presentes.

Sintaxis:

public String nextLine()>

Parámetros: La función no acepta ningún parámetro.



Valor de retorno: Este método devuelve el línea eso fue omitido

Excepciones: La función genera dos excepciones como se describe a continuación:

    NoSuchElementException: se lanza si no se encontró ninguna línea. IllegalStateException: se lanza si este escáner está cerrado.

Los siguientes programas ilustran la función anterior:

10ml en onzas

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

>

>

Producción:

 Gfg Geeks GeeksForGeeks>

Programa 2: Para demostrar NoSuchElementException




mvc en marco de primavera
// 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);> >}> >}> }>

>

>

Producción:

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

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

ups
>

>

Producción:

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

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