logo

Metoda Java Scanner useDelimiter().

The koristirazdjelnik() je metoda klase Java skenera koja se koristi za postavljanje razgraničavajućeg uzorka skenera koji se koristi. Postoje dvije različite vrste Java metode useDelimiter() koje se mogu razlikovati ovisno o parametru. Ovi su:

  • Metoda Java Scanner useDelimiter(Pattern pattern).
  • Metoda Java Scanner useDelimiter(String pattern).

Sintaksa

Slijedi izjava od koristirazdjelnik() metoda:

 public Scanner useDelimiter(Pattern pattern) public Scanner useDelimiter(String pattern) 

Parametar

Tip podataka Parametar Opis Obavezno/opcionalno
Uzorak uzorak To je uzorak razgraničenja Potreban
Niz uzorak To je niz koji specificira uzorak razgraničenja. Potreban

Povratak

The koristirazdjelnik() metoda vraća ovaj objekt skenera.

pretvaranje niza u int

Iznimke

DA

datoteka otvorena u Javi

Verzija kompatibilnosti

Java 1.5 i novije

Primjer 1

 import java.util.Scanner; public class ScannerUseDelimiterExample1 { public static void main(String args[]){ String str = 'JavaTpoint! 13 + 13.0 = 26.0 false '; //Create scanner with the specified String Object Scanner scanner = new Scanner(str); //Print String System.out.println('String: ' + scanner.nextLine()); //Change the delimiter of this scanner scanner.useDelimiter('vaT'); //Display the new delimiter System.out.println('New delimiter: ' +scanner.delimiter()); scanner.close(); } } 

Izlaz:

 String: JavaTpoint! 13 + 13.0 = 26.0 false New delimiter: vaT 

Primjer 2

 import java.util.Scanner; public class ScannerUseDelimiterExample2 { public static void main(String args[]){ // Initialize Scanner object Scanner scan = new Scanner('JavaTpoint/Abhishek/Male/22'); //Initialize the string delimiter scan.useDelimiter('/'); //Printing the tokenized Strings while(scan.hasNext()){ System.out.println(scan.next()); } scan.close(); } } 

Izlaz:

 JavaTpoint Abhishek Male 22 

Primjer 3

 import java.util.Scanner; public class ScannerUseDelimiterExample3 { public static void main(String args[]){ String input = '1 fish 2 fish red fish blue fish'; // \s* means 0 or more repetitions of any whitespace character // fish is the pattern to find @SuppressWarnings('resource') Scanner sc = new Scanner(input).useDelimiter('\s*fish\s*'); System.out.println(sc.nextInt()); // prints: 1 System.out.println(sc.nextInt()); // prints: 2 System.out.println(sc.next()); // prints: red System.out.println(sc.next()); // prints: blue //close the scanner sc.close(); } } 

Izlaz:

 1 2 red blue 

Primjer 3

 import java.util.Scanner; public class ScannerUseDelimiterExample4 { public static void main(String args[]){ //Create scanner with the specified String Object Scanner scanner = new Scanner('55 13 + 13.0 = 26.0 77'); //Print String System.out.println('String: ' + scanner.nextLine()); //Change the delimiter of this scanner System.out.println('New delimiter: ' +scanner.useDelimiter('abcd')); scanner.close(); } } 

Izlaz:

veličina vektora c++
 String: 55 13 + 13.0 = 26.0 77 New delimiter: java.util.Scanner[delimiters=abcd][position=22][match valid=true][need input=false][source closed=true][skipped=false][group separator=,][decimal separator=.][positive prefix=][negative prefix=Q-E][positive suffix=][negative suffix=][NaN string=QNaNE][infinity string=Q∞E]