logo

Metoda skanera nextLine() w Javie z przykładami

The następna linia() metoda Skaner java.util class przesuwa skaner poza bieżący wiersz i zwraca pominięte dane wejściowe. Ta funkcja drukuje resztę bieżącej linii, pomijając separator linii na końcu. Następny jest ustawiony po separatorze linii. Ponieważ ta metoda w dalszym ciągu przeszukuje dane wejściowe w poszukiwaniu separatora linii, może przeszukać całe wejście w poszukiwaniu linii do pominięcia, jeśli nie ma żadnych separatorów linii.

Składnia:



public String nextLine()>

Parametry: Funkcja nie przyjmuje żadnego parametru.

Wartość zwracana: Ta metoda zwraca linia to zostało pominięte

Wyjątki: Funkcja zgłasza dwa wyjątki, jak opisano poniżej:



    NoSuchElementException: zgłasza, jeśli nie znaleziono żadnej linii. IllegalStateException: zgłasza, jeśli ten skaner jest zamknięty

Poniższe programy ilustrują powyższą funkcję:

Program 1:




rozmiary czcionek lateksowych



program macierzowy w języku c
// 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();> >}> }>

>

>

Wyjście:

 Gfg Geeks GeeksForGeeks>

Program 2: Aby zademonstrować wyjątek NoSuchElementException


polecenie git push



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

>

>

Wyjście:

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

Program 3: Aby zademonstrować wyjątek 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);> >}> >}> }>

string.compare C#

>

>

Wyjście:

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

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