logo

Scanner nextLine()-methode in Java met voorbeelden

De volgenderegel() methode van java.util.Scanner class beweegt deze scanner voorbij de huidige regel en retourneert de invoer die is overgeslagen. Deze functie drukt de rest van de huidige regel af, waarbij het regelscheidingsteken aan het einde wordt weggelaten. De volgende wordt ingesteld na het regelscheidingsteken. Aangezien deze methode doorgaat met het doorzoeken van de invoer op zoek naar een regelscheidingsteken, kan deze de gehele invoer doorzoeken op zoek naar de regel die moet worden overgeslagen als er geen regelscheidingstekens aanwezig zijn.

Syntaxis:



public String nextLine()>

Parameters: De functie accepteert geen enkele parameter.

Winstwaarde: Deze methode retourneert de lijn dat werd overgeslagen

Uitzonderingen: De functie genereert twee uitzonderingen, zoals hieronder beschreven:



    NoSuchElementException: genereert als er geen regel is gevonden IllegalStateException: genereert als deze scanner is gesloten

Onderstaande programma's illustreren de bovenstaande functie:

Programma 1:






if door Rudyard Kipling regel voor regel uitleg

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

>

>

Uitgang:

 Gfg Geeks GeeksForGeeks>

Programma 2: Om NoSuchElementException te demonstreren




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

>

>

Uitgang:

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

Programma 3: Om IllegalStateException aan te tonen




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

hoe je game pigeon op Android kunt krijgen
>

>

Uitgang:

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

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