logo

Metoda skeneru nextLine() v Javě s příklady

The další řádek() metoda java.util.Scanner class posune tento skener za aktuální řádek a vrátí vstup, který byl přeskočen. Tato funkce vytiskne zbytek aktuálního řádku s vynecháním oddělovače řádku na konci. Další je nastaveno za oddělovač řádků. Protože tato metoda pokračuje v prohledávání vstupu a hledá oddělovač řádků, může prohledávat všechny vstupy a hledat řádek, který se má přeskočit, pokud nejsou přítomny žádné oddělovače řádků.

Syntax:



public String nextLine()>

Parametry: Funkce nepřijímá žádný parametr.

Návratová hodnota: Tato metoda vrací čára to bylo přeskočeno

Výjimky: Funkce vyvolá dvě výjimky, jak je popsáno níže:



    NoSuchElementException: vyvolá, pokud nebyl nalezen žádný řádek, IllegalStateException: vyvolá, pokud je tento skener zavřený

Níže uvedené programy ilustrují výše uvedenou funkci:

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

rozdíl tygří lev

>

>

Výstup:

 Gfg Geeks GeeksForGeeks>

Program 2: Chcete-li demonstrovat 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);> >}> >}> }>

>

>

Výstup:

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

Program 3: Pro demonstraci 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);> >}> >}> }>

python je číselný
>

>

Výstup:

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

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