logo

Metoda Java Scanner hasNext().

The hasNext() je metoda klase Java Scanner koja vraća true ako ovaj skener ima drugi token u svom ulazu. Tamo su tri različite vrste Java Scannera hasNext() metoda koja se može razlikovati ovisno o njezinom parametru. Ovi su:

  1. Metoda Java Scanner hasNext ().
  2. Metoda Java Scanner hasNext (String pattern).
  3. Metoda Java Scanner hasNext(Pattern pattern).

1. Java Scanner hasNext () Metoda:

To je metoda klase Scanner koja vraća true ako ovaj skener ima drugi token u svom ulazu. Ova metoda može blokirati dok se čeka unos za skeniranje.

2.Java Scanner hasNext (String pattern) Metoda:

To je metoda klase Scanner koja vraća true ako sljedeći token odgovara uzorku konstruiranom iz navedenog niza.

globalne varijable js

3. Metoda Java Scanner hasNext (uzorak uzorka):

To je metoda klase Scanner koja vraća true ako sljedeći potpuni token odgovara navedenom uzorku.

Sintaksa

Slijede izjave od hasNext() metoda:

 public boolean hasNext() public boolean hasNext(String pattern) public boolean hasNext(Pattern pattern) 

Parametar

Tip podataka Parametar Opis Obavezno/opcionalno
Niz uzorak To je niz koji navodi uzorak za skeniranje. Potreban
Uzorak uzorak To je obrazac za skeniranje navedenog niza. Potreban

Povratak

metoda Povratak
hasNext() Ova metoda vraća true ako i samo ako ovaj skener ima drugi token.
hasNext(uzorak niza) Ova metoda vraća true ako i samo ako ovaj skener ima drugi token koji odgovara navedenom uzorku.
hasNext(uzorak uzorka) Ova metoda vraća true ako i samo ako ovaj skener ima drugi token koji odgovara navedenom uzorku.

Iznimke

IllegalStateException - Ova metoda baca iznimku ako se pozivanje izvrši nakon što je skener zatvoren.

Verzija kompatibilnosti

Java 1.5 i novije

mylivecricket alternativa

Primjer 1

 import java.util.*; public class ScannerHasNextExample1 { public static void main(String args[]){ //Create Scanner object Scanner scan = new Scanner('Hello World!'); //Printing the delimiter used System.out.println('Delimiter:' + scan.delimiter()); //Print the Strings while (scan.hasNext()) { System.out.println(scan.next()); } //Close the scanner scan.close(); } } 

Izlaz:

 Delimiter:p{javaWhitespace}+ Hello World! 

Primjer 2

 import java.util.*; public class ScannerHasNextExample2 { public static void main(String args[]){ String s = 'Hello, This is JavaTpoint.'; //Create scanner Object and pass string in it Scanner scan = new Scanner(s); //Check if the scanner has a token System.out.println('Result: ' + scan.hasNext()); //Print the string System.out.println('String: ' +scan.nextLine()); //Check if the scanner has a token after printing the line System.out.println('Final Result: ' + scan.hasNext()); //Close the scanner scan.close(); } } 

Izlaz:

 Result: true String: Hello, This is JavaTpoint. Final Result: false 

Primjer 3

 import java.util.*; public class ScannerHasNextExample3 { public static void main(String args[]){ //Create Scanner object Scanner scan = new Scanner('Program:Java;Python;Android'); //Initialize the String pattern String pattern = 'Program:.*'; //Check if pattern satisfies the String content if(scan.hasNext(pattern)){ System.out.println('Pattern found'); } else{ System.out.println('Pattern not found'); } scan.close(); } } 

Izlaz:

 Pattern found 

Primjer 4

 import java.util.*; public class ScannerHasNextExample4 { public static void main(String args[]){ String str = 'JavaTpoint.com 15 + 15 = 18.0'; Scanner scanner = new Scanner(str); //Checking scanner's next token matches 'c' System.out.println('Result: '+scanner.hasNext('JavaTpoint.com')); //Checking scanner's next token matches '=' System.out.println('Result: '+scanner.hasNext('=')); //Print the rest of the string System.out.println('Rest of String: '+scanner.nextLine()); scanner.close(); } } 

Izlaz:

 Result: true Result: false Rest of String: JavaTpoint.com 15 + 15 = 18.0 

Primjer 5

 import java.util.*; import java.util.regex.Pattern; public class ScannerHasNextExample5 { public static void main(String args[]){ //Create Scanner object Scanner scan = new Scanner('Names:Raju1;Pawan;Suresh'); //Declare the delimiter scan.useDelimiter(';'); /*Initialize the String pattern which signifies that the String token contains characters of the alphabet only*/ Pattern pattern = Pattern.compile('[A-Za-z]*'); while(scan.hasNext()){ //Check if the token consists of declared pattern if(scan.hasNext(pattern)){ System.out.println(scan.next()); } else scan.next(); } scan.close(); } } 

Izlaz:

 Pawan Suresh