Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a delimiter with Scanner.useDelimiter in Java?

sc = new Scanner(new File(dataFile)); sc.useDelimiter(",|\r\n"); 

I don't understand how delimiter works, can someone explain this in layman terms?

like image 691
NoMoreErrors Avatar asked Feb 27 '15 13:02

NoMoreErrors


People also ask

How do you use a delimiter Scanner?

The scanner can also use delimiters other than whitespace. Easy example from Scanner API: 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 Scanner s = new Scanner(input). useDelimiter("\\s*fish\\s*"); System.

What are delimiters in Scanner class?

The delimiter() is a method of Java Scanner class which is used to get the Pattern which the Scanner class is currently using to match delimiters.

Which of these methods is used for setting the delimiting pattern to a pattern?

useDelimiter(String pattern) method Sets this scanner's delimiting pattern to a pattern constructed from the specified String.


2 Answers

The scanner can also use delimiters other than whitespace.

Easy example from Scanner API:

 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  Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");   System.out.println(s.nextInt());   // prints: 1  System.out.println(s.nextInt());   // prints: 2  System.out.println(s.next());      // prints: red  System.out.println(s.next());      // prints: blue   // don't forget to close the scanner!!  s.close();  

The point is to understand the regular expressions (regex) inside the Scanner::useDelimiter. Find an useDelimiter tutorial here.


To start with regular expressions here you can find a nice tutorial.

Notes

abc…    Letters 123…    Digits \d      Any Digit \D      Any Non-digit character .       Any Character \.      Period [abc]   Only a, b, or c [^abc]  Not a, b, nor c [a-z]   Characters a to z [0-9]   Numbers 0 to 9 \w      Any Alphanumeric character \W      Any Non-alphanumeric character {m}     m Repetitions {m,n}   m to n Repetitions *       Zero or more repetitions +       One or more repetitions ?       Optional character \s      Any Whitespace \S      Any Non-whitespace character ^…$     Starts and ends (…)     Capture Group (a(bc)) Capture Sub-group (.*)    Capture all (ab|cd) Matches ab or cd 
like image 183
Jordi Castilla Avatar answered Sep 23 '22 08:09

Jordi Castilla


With Scanner the default delimiters are the whitespace characters.

But Scanner can define where a token starts and ends based on a set of delimiter, wich could be specified in two ways:

  1. Using the Scanner method: useDelimiter(String pattern)
  2. Using the Scanner method : useDelimiter(Pattern pattern) where Pattern is a regular expression that specifies the delimiter set.

So useDelimiter() methods are used to tokenize the Scanner input, and behave like StringTokenizer class, take a look at these tutorials for further information:

  • Setting Delimiters for Scanner
  • Java.util.Scanner.useDelimiter() Method

And here is an Example:

public static void main(String[] args) {      // Initialize Scanner object     Scanner scan = new Scanner("Anna Mills/Female/18");     // initialize the string delimiter     scan.useDelimiter("/");     // Printing the tokenized Strings     while(scan.hasNext()){         System.out.println(scan.next());     }     // closing the scanner stream     scan.close(); } 

Prints this output:

Anna Mills Female 18 
like image 32
cнŝdk Avatar answered Sep 21 '22 08:09

cнŝdk