The useDelimiter() ir Java skenera klases metode, ko izmanto, lai iestatītu izmantojamā skenera norobežojošo modeli. Ir divi dažādi Java useDelimiter() metodes veidi, kurus var atšķirt atkarībā no tā parametra. Šie ir:
- Java skeneris izmanto atdalīšanas (raksta raksta) metodi
- Java skeneris izmanto atdalīšanas metodi (virknes raksts).
Sintakse
Tālāk ir sniegta deklarācija useDelimiter() metode:
public Scanner useDelimiter(Pattern pattern) public Scanner useDelimiter(String pattern)
Parametrs
Datu tips | Parametrs | Apraksts | Obligāti/neobligāti |
---|---|---|---|
Raksts | modelis | Tas ir norobežojošs modelis | Obligāti |
Stīga | modelis | Tā ir virkne, kas norāda norobežojošo modeli. | Obligāti |
Atgriežas
The useDelimiter() metode atgriež šo skenera objektu.
Excel noņemt pirmo rakstzīmi
Izņēmumi
TAS
Charat virknē
Saderības versija
Java 1.5 un jaunāka versija
1. piemērs
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(); } }
Izvade:
String: JavaTpoint! 13 + 13.0 = 26.0 false New delimiter: vaT
2. piemērs
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(); } }
Izvade:
JavaTpoint Abhishek Male 22
3. piemērs
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(); } }
Izvade:
1 2 red blue
3. piemērs
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(); } }
Izvade:
auns aktieris
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]