De nextLine() Metode av java.util.Scanner klasse flytter denne skanneren forbi gjeldende linje og returnerer inndataene som ble hoppet over. Denne funksjonen skriver ut resten av gjeldende linje, og utelater linjeskilletegn på slutten. Den neste er satt til etter linjeskilleren. Siden denne metoden fortsetter å søke gjennom inngangen og leter etter en linjeseparator, kan den søke etter alle inndataene som søker etter linjen som skal hoppes over hvis ingen linjeskillere er til stede.
Syntaks:
public String nextLine()>
Parametere: Funksjonen godtar ingen parameter.
ikke null i js
Returverdi: Denne metoden returnerer linje som ble hoppet over
Unntak: Funksjonen gir to unntak som beskrevet nedenfor:
- NoSuchElementException: kaster hvis ingen linje ble funnet IllegalStateException: kaster hvis denne skanneren er lukket
Programmene nedenfor illustrerer funksjonen ovenfor:
Program 1:
full 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();> >}> }> |
>
>Produksjon:
Gfg Geeks GeeksForGeeks>
Program 2: For å demonstrere NoSuchElementException
fil åpen i java
// 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 lastet nedProduksjon:
Exception thrown: java.util.NoSuchElementException: No line found>
Program 3: For å demonstrere IllegalStateException
lære selen
// 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);> >}> >}> }> |
>
>Produksjon:
Exception thrown: java.lang.IllegalStateException: Scanner closed>
Henvisning: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()