logo

Java Scanner useDelimiter() Method

The useDelimiter() je metoda razreda Java Scanner, ki se uporablja za nastavitev razmejitvenega vzorca Scannerja, ki je v uporabi. Obstajata dve različni vrsti metode Java useDelimiter(), ki ju je mogoče razlikovati glede na njen parameter. To so:

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

Sintaksa

Sledi izjava useDelimiter() metoda:

markdown prečrtano
 public Scanner useDelimiter(Pattern pattern) public Scanner useDelimiter(String pattern) 

Parameter

DataType Parameter Opis Obvezno/izbirno
Vzorec vzorec Je razmejitveni vzorec Obvezno
Vrvica vzorec To je niz, ki določa razmejitveni vzorec. Obvezno

Vračila

The useDelimiter() metoda vrne ta objekt optičnega bralnika.

Izjeme

TO

powershell manjše ali enako

Združljivostna različica

Java 1.5 in novejši

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

Izhod:

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

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

Izhod:

avl drevesa
 JavaTpoint Abhishek Male 22 

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

Izhod:

 1 2 red blue 

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

Izhod:

 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]