logo

Metoda Scanner nextLine() v Javi s primeri

The naslednja vrstica() metoda java.util.Scanner razred premakne ta optični bralnik mimo trenutne vrstice in vrne vnos, ki je bil preskočen. Ta funkcija natisne preostanek trenutne vrstice, pri čemer izpusti ločilo vrstic na koncu. Naslednji je nastavljen na za ločilom vrstic. Ker ta metoda nadaljuje z iskanjem po vnosu in išče ločilo vrstic, lahko preišče ves vnos in išče vrstico za preskok, če ni nobenih ločil vrstic.

Sintaksa:



public String nextLine()>

Parametri: Funkcija ne sprejema nobenega parametra.

sortiraj seznam matrik java

Povratna vrednost: Ta metoda vrne linija ki je bil preskočen

Izjeme: Funkcija vrže dve izjemi, kot je opisano spodaj:



    NoSuchElementException: vrže, če ni bila najdena nobena vrstica IllegalStateException: vrže, če je ta skener zaprt

Spodnji programi ponazarjajo zgornjo funkcijo:

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

>

>

Izhod:

 Gfg Geeks GeeksForGeeks>

Program 2: Za prikaz NoSuchElementException




python je numerični
// 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);> >}> >}> }>

>

>

Izhod:

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

Program 3: Za prikaz 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);> >}> >}> }>

kaj je gb
>

>

Izhod:

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

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