logo

Metoda Java Scanner nextLine().

The naslednja vrstica() Metoda razreda Java Scanner se uporablja za pridobitev vhodnega niza, ki je bil preskočen v objektu Scanner.

Sintaksa

Sledi izjava naslednja vrstica() metoda:

 public String nextLine() 

Parameter

Ta metoda ne sprejema nobenega parametra.

Vračila

The naslednja vrstica() metoda vrne vrstico, ki je bila preskočena.

Izjeme

NoSuchElementException - Če ne najde nobene vrstice, bo sprožil to izjemo.

IllegalStateException - To izjemo bo sprožil, če se bo priklic izvedel po zaprtju Scannerja.

kaj je const v Javi

Združljivostna različica

Java 1.5 in novejši

Primer 1

 import java.util.*; public class ScannerNextLineExample1 { public static void main(String args[]){ Scanner scan = new Scanner(System.in); System.out.print('Enter Item ID: '); String itemID = scan.nextLine(); System.out.print('Enter Item price: '); String priceStr = scan.nextLine(); double price = Double.valueOf(priceStr); System.out.println('Price of Item '+itemID + ' is $'+price); scan.close(); } } 

Izhod:

 Enter Item ID: A151 Enter Item price: 3473.75 Price of Item A151 is 73.75 

Primer 2

 import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class ScannerNextLineExample2 { public static void main(String args[]) throws FileNotFoundException{ // Declare File object File file = new File('/home/javatpoint/Desktop/ABHISHEK/AngularJS/Index/abc.txt'); // initialize the scanner Scanner scan = new Scanner(file); // iterate through the file line by line while(scan.hasNextLine()){ // print the contents of a file by line System.out.println(scan.nextLine()); } // close the scanner object; scan.close(); } } 

Izhod:

 hasNextLine public boolean hasNextLine() IllegalStateException 

Primer 3

 import java.util.*; public class ScannerNextLineExample3 { public static void main(String args[]){ String str = 'Facebook.com 
 1 + 1 = 2.0 
 JavaTpoint.com '; Scanner scanner = new Scanner(str); //Print the next line System.out.println(scanner.nextLine()); //Check if there is a next line again System.out.println(scanner.hasNextLine()); System.out.println(scanner.nextLine()); //Check if there is a next line again System.out.println(scanner.hasNextLine()); System.out.println(scanner.nextLine()); //Check if there is a next line again System.out.println(scanner.hasNextLine()); scanner.close(); } } 

Izhod:

 Facebook.com true 1 + 1 = 2.0 true JavaTpoint.com false