logo

Scanner nextLine() metode i Java med eksempler

Det næste linje() metode til java.util.Scanner klasse flytter denne scanner forbi den aktuelle linje og returnerer det input, der blev sprunget over. Denne funktion udskriver resten af ​​den aktuelle linje og udelader linjeseparatoren i slutningen. Den næste er sat til efter linjeseparatoren. Da denne metode fortsætter med at søge gennem inputtet på udkig efter en linjeseparator, kan den søge i hele inputtet, der søger efter den linje, der skal springes over, hvis der ikke er nogen linjeseparatorer til stede.

Syntaks:



public String nextLine()>

Parametre: Funktionen accepterer ingen parameter.

ikke null i js

Returneringsværdi: Denne metode returnerer linje der blev sprunget over

Undtagelser: Funktionen kaster to undtagelser som beskrevet nedenfor:



    NoSuchElementException: kaster hvis ingen linje blev fundet IllegalStateException: kaster hvis denne scanner er lukket

Nedenstående programmer illustrerer ovenstående funktion:

Program 1:






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

>

>

Produktion:

 Gfg Geeks GeeksForGeeks>

Program 2: For at demonstrere NoSuchElementException

inkludere c programmering




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

>

>

javascript onload
Produktion:

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

Program 3: For at demonstrere IllegalStateException


hvordan man konverterer streng til heltal



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

>

>

Produktion:

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

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