Der nächste Zeile() Methode von java.util.Scanner Die Klasse rückt diesen Scanner über die aktuelle Zeile hinaus vor und gibt die übersprungene Eingabe zurück. Diese Funktion druckt den Rest der aktuellen Zeile und lässt den Zeilentrenner am Ende weg. Der nächste wird nach dem Zeilentrennzeichen gesetzt. Da diese Methode weiterhin die Eingabe nach einem Zeilentrennzeichen durchsucht, durchsucht sie möglicherweise die gesamte Eingabe nach der Zeile, die übersprungen werden soll, wenn keine Zeilentrennzeichen vorhanden sind.
Syntax:
public String nextLine()>
Parameter: Die Funktion akzeptiert keine Parameter.
Rückgabewert: Diese Methode gibt die zurück Linie das wurde übersprungen
Ausnahmen: Die Funktion löst zwei Ausnahmen aus, wie unten beschrieben:
- NoSuchElementException: wird ausgelöst, wenn keine Zeile gefunden wurde. IllegalStateException: wird ausgelöst, wenn dieser Scanner geschlossen ist
Die folgenden Programme veranschaulichen die obige Funktion:
Programm 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();> >}> }> |
>
>Ausgabe:
Gfg Geeks GeeksForGeeks>
Programm 2: Zur Demonstration von NoSuchElementException
nicht gleich MySQL
// 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);> >}> >}> }> |
>
>Ausgabe:
Exception thrown: java.util.NoSuchElementException: No line found>
Programm 3: Zur Demonstration von 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);> >}> >}> }> |
>
>Ausgabe:
Exception thrown: java.lang.IllegalStateException: Scanner closed>
Referenz: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()