logo

Java Scanner nextLine() metoda

The sljedeći redak() metoda klase Java Scanner koristi se za dobivanje ulaznog niza koji je preskočen u objektu Scanner.

Sintaksa

Slijedi izjava od sljedeći redak() metoda:

 public String nextLine() 

Parametar

Ova metoda ne prihvaća nijedan parametar.

Povratak

The sljedeći redak() metoda vraća redak koji je preskočen.

Iznimke

NoSuchElementException - Izbacit će ovu iznimku ako linija nije pronađena.

IllegalStateException - Izbacit će ovu iznimku ako se pozivanje izvrši nakon zatvaranja Scannera.

tko je freddie mercury

Verzija kompatibilnosti

Java 1.5 i novije

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

Izlaz:

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

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

Izlaz:

 hasNextLine public boolean hasNextLine() IllegalStateException 

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

Izlaz:

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